2 * util/netevent.h - event notification
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains event notification functions.
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.
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.
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.
63 #include "dnscrypt/dnscrypt.h"
70 /* internal event notification data storage structure. */
71 struct internal_event;
73 struct internal_timer; /* A sub struct of the comm_timer super struct */
75 /** callback from communication point function type */
76 typedef int comm_point_callback_type(struct comm_point*, void*, int,
79 /** to pass no_error to callback function */
80 #define NETEVENT_NOERROR 0
81 /** to pass closed connection to callback function */
82 #define NETEVENT_CLOSED -1
83 /** to pass timeout happened to callback function */
84 #define NETEVENT_TIMEOUT -2
85 /** to pass fallback from capsforID to callback function; 0x20 failed */
86 #define NETEVENT_CAPSFAIL -3
88 /** timeout to slow accept calls when not possible, in msec. */
89 #define NETEVENT_SLOW_ACCEPT_TIME 2000
92 * A communication point dispatcher. Thread specific.
95 /** behind the scenes structure. with say libevent info. alloced */
96 struct internal_base* eb;
97 /** callback to stop listening on accept sockets,
98 * performed when accept() will not function properly */
99 void (*stop_accept)(void*);
100 /** callback to start listening on accept sockets, performed
101 * after stop_accept() then a timeout has passed. */
102 void (*start_accept)(void*);
103 /** user argument for stop_accept and start_accept functions */
108 * Reply information for a communication point.
111 /** the comm_point with fd to send reply on to. */
112 struct comm_point* c;
113 /** the address (for UDP based communication) */
114 struct sockaddr_storage addr;
115 /** length of address */
117 /** return type 0 (none), 4(IP4), 6(IP6) */
119 /* DnsCrypt context */
121 uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
122 uint8_t nmkey[crypto_box_BEFORENMBYTES];
123 const dnsccert *dnsc_cert;
126 /** the return source interface data */
129 struct in6_pktinfo v6info;
132 struct in_pktinfo v4info;
133 #elif defined(IP_RECVDSTADDR)
134 struct in_addr v4addr;
137 /** variable with return source data */
139 /** max udp size for udp packets */
144 * Communication point to the network
145 * These behaviours can be accomplished by setting the flags
146 * and passing return values from the callback.
147 * udp frontside: called after readdone. sendafter.
148 * tcp frontside: called readdone, sendafter. close.
149 * udp behind: called after readdone. No send after.
150 * tcp behind: write done, read done, then called. No send after.
153 /** behind the scenes structure, with say libevent info. alloced. */
154 struct internal_event* ev;
156 /** file descriptor for communication point */
159 /** timeout (NULL if it does not). Malloced. */
160 struct timeval* timeout;
162 /** buffer pointer. Either to perthread, or own buffer or NULL */
163 struct sldns_buffer* buffer;
165 /* -------- TCP Handler -------- */
166 /** Read/Write state for TCP */
168 /** The current read/write count for TCP */
169 size_t tcp_byte_count;
170 /** parent communication point (for TCP sockets) */
171 struct comm_point* tcp_parent;
172 /** sockaddr from peer, for TCP handlers */
173 struct comm_reply repinfo;
175 /* -------- TCP Accept -------- */
176 /** the number of TCP handlers for this tcp-accept socket */
178 /** current number of tcp handler in-use for this accept socket */
180 /** malloced array of tcp handlers for a tcp-accept,
181 of size max_tcp_count. */
182 struct comm_point** tcp_handlers;
183 /** linked list of free tcp_handlers to use for new queries.
184 For tcp_accept the first entry, for tcp_handlers the next one. */
185 struct comm_point* tcp_free;
187 /* -------- SSL TCP DNS ------- */
188 /** the SSL object with rw bio (owned) or for commaccept ctx ref */
190 /** handshake state for init and renegotiate */
192 /** no handshake, it has been done */
193 comm_ssl_shake_none = 0,
194 /** ssl initial handshake wants to read */
196 /** ssl initial handshake wants to write */
197 comm_ssl_shake_write,
198 /** ssl_write wants to read */
199 comm_ssl_shake_hs_read,
200 /** ssl_read wants to write */
201 comm_ssl_shake_hs_write
204 /* -------- dnstap ------- */
205 /** the dnstap environment */
206 struct dt_env* dtenv;
208 /** is this a UDP, TCP-accept or TCP socket. */
209 enum comm_point_type {
210 /** UDP socket - handle datagrams. */
212 /** TCP accept socket - only creates handlers if readable. */
214 /** TCP handler socket - handle byteperbyte readwrite. */
216 /** AF_UNIX socket - for internal commands. */
218 /** raw - not DNS format - for pipe readers and writers */
221 /** variable with type of socket, UDP,TCP-accept,TCP,pipe */
224 /* ---------- Behaviour ----------- */
225 /** if set the connection is NOT closed on delete. */
228 /** if set, the connection is closed on error, on timeout,
229 and after read/write completes. No callback is done. */
232 /** if set, read/write completes:
233 read/write state of tcp is toggled.
234 buffer reset/bytecount reset.
236 So that when that is done the callback is called. */
237 int tcp_do_toggle_rw;
239 /** timeout in msec for TCP wait times for this connection */
240 int tcp_timeout_msec;
242 /** if set, checks for pending error from nonblocking connect() call.*/
243 int tcp_check_nb_connect;
245 #ifdef USE_MSG_FASTOPEN
246 /** used to track if the sendto() call should be done when using TFO. */
251 /** Is this a dnscrypt channel */
253 /** encrypted buffer pointer. Either to perthread, or own buffer or NULL */
254 struct sldns_buffer* dnscrypt_buffer;
256 /** number of queries outstanding on this socket, used by
257 * outside network for udp ports */
260 /** callback when done.
261 tcp_accept does not get called back, is NULL then.
262 If a timeout happens, callback with timeout=1 is called.
263 If an error happens, callback is called with error set
264 nonzero. If not NETEVENT_NOERROR, it is an errno value.
265 If the connection is closed (by remote end) then the
266 callback is called with error set to NETEVENT_CLOSED=-1.
267 If a timeout happens on the connection, the error is set to
269 The reply_info can be copied if the reply needs to happen at a
270 later time. It consists of a struct with commpoint and address.
271 It can be passed to a msg send routine some time later.
272 Note the reply information is temporary and must be copied.
273 NULL is passed for_reply info, in cases where error happened.
276 int my_callback(struct comm_point* c, void* my_arg, int error,
277 struct comm_reply *reply_info);
279 if the routine returns 0, nothing is done.
280 Notzero, the buffer will be sent back to client.
281 For UDP this is done without changing the commpoint.
282 In TCP it sets write state.
284 comm_point_callback_type* callback;
285 /** argument to pass to callback. */
290 * Structure only for making timeout events.
293 /** the internal event stuff (derived) */
294 struct internal_timer* ev_timer;
296 /** callback function, takes user arg only */
297 void (*callback)(void*);
299 /** callback user argument */
304 * Structure only for signal events.
307 /** the communication base */
308 struct comm_base* base;
310 /** the internal event stuff */
311 struct internal_signal* ev_signal;
313 /** callback function, takes signal number and user arg */
314 void (*callback)(int, void*);
316 /** callback user argument */
321 * Create a new comm base.
322 * @param sigs: if true it attempts to create a default loop for
324 * @return: the new comm base. NULL on error.
326 struct comm_base* comm_base_create(int sigs);
329 * Create comm base that uses the given ub_event_base (underlying pluggable
330 * event mechanism pointer).
331 * @param base: underlying pluggable event base.
332 * @return: the new comm base. NULL on error.
334 struct comm_base* comm_base_create_event(struct ub_event_base* base);
337 * Delete comm base structure but not the underlying lib event base.
338 * All comm points must have been deleted.
339 * @param b: the base to delete.
341 void comm_base_delete_no_base(struct comm_base* b);
344 * Destroy a comm base.
345 * All comm points must have been deleted.
346 * @param b: the base to delete.
348 void comm_base_delete(struct comm_base* b);
351 * Obtain two pointers. The pointers never change (until base_delete()).
352 * The pointers point to time values that are updated regularly.
353 * @param b: the communication base that will update the time values.
354 * @param tt: pointer to time in seconds is returned.
355 * @param tv: pointer to time in microseconds is returned.
357 void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
360 * Dispatch the comm base events.
361 * @param b: the communication to perform.
363 void comm_base_dispatch(struct comm_base* b);
366 * Exit from dispatch loop.
367 * @param b: the communication base that is in dispatch().
369 void comm_base_exit(struct comm_base* b);
372 * Set the slow_accept mode handlers. You can not provide these if you do
373 * not perform accept() calls.
374 * @param b: comm base
375 * @param stop_accept: function that stops listening to accept fds.
376 * @param start_accept: function that resumes listening to accept fds.
377 * @param arg: callback arg to pass to the functions.
379 void comm_base_set_slow_accept_handlers(struct comm_base* b,
380 void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
383 * Access internal data structure (for util/tube.c on windows)
384 * @param b: comm base
385 * @return ub_event_base.
387 struct ub_event_base* comm_base_internal(struct comm_base* b);
390 * Create an UDP comm point. Calls malloc.
391 * setups the structure with the parameters you provide.
392 * @param base: in which base to alloc the commpoint.
393 * @param fd : file descriptor of open UDP socket.
394 * @param buffer: shared buffer by UDP sockets from this thread.
395 * @param callback: callback function pointer.
396 * @param callback_arg: will be passed to your callback function.
397 * @return: returns the allocated communication point. NULL on error.
398 * Sets timeout to NULL. Turns off TCP options.
400 struct comm_point* comm_point_create_udp(struct comm_base* base,
401 int fd, struct sldns_buffer* buffer,
402 comm_point_callback_type* callback, void* callback_arg);
405 * Create an UDP with ancillary data comm point. Calls malloc.
406 * Uses recvmsg instead of recv to get udp message.
407 * setups the structure with the parameters you provide.
408 * @param base: in which base to alloc the commpoint.
409 * @param fd : file descriptor of open UDP socket.
410 * @param buffer: shared buffer by UDP sockets from this thread.
411 * @param callback: callback function pointer.
412 * @param callback_arg: will be passed to your callback function.
413 * @return: returns the allocated communication point. NULL on error.
414 * Sets timeout to NULL. Turns off TCP options.
416 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
417 int fd, struct sldns_buffer* buffer,
418 comm_point_callback_type* callback, void* callback_arg);
421 * Create a TCP listener comm point. Calls malloc.
422 * Setups the structure with the parameters you provide.
423 * Also Creates TCP Handlers, pre allocated for you.
424 * Uses the parameters you provide.
425 * @param base: in which base to alloc the commpoint.
426 * @param fd: file descriptor of open TCP socket set to listen nonblocking.
427 * @param num: becomes max_tcp_count, the routine allocates that
428 * many tcp handler commpoints.
429 * @param bufsize: size of buffer to create for handlers.
430 * @param callback: callback function pointer for TCP handlers.
431 * @param callback_arg: will be passed to your callback function.
432 * @return: returns the TCP listener commpoint. You can find the
433 * TCP handlers in the array inside the listener commpoint.
434 * returns NULL on error.
435 * Inits timeout to NULL. All handlers are on the free list.
437 struct comm_point* comm_point_create_tcp(struct comm_base* base,
438 int fd, int num, size_t bufsize,
439 comm_point_callback_type* callback, void* callback_arg);
442 * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
443 * @param base: in which base to alloc the commpoint.
444 * @param bufsize: size of buffer to create for handlers.
445 * @param callback: callback function pointer for the handler.
446 * @param callback_arg: will be passed to your callback function.
447 * @return: the commpoint or NULL on error.
449 struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
450 size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
453 * Create commpoint to listen to a local domain file descriptor.
454 * @param base: in which base to alloc the commpoint.
455 * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
456 * @param bufsize: size of buffer to create for handlers.
457 * @param callback: callback function pointer for the handler.
458 * @param callback_arg: will be passed to your callback function.
459 * @return: the commpoint or NULL on error.
461 struct comm_point* comm_point_create_local(struct comm_base* base,
462 int fd, size_t bufsize,
463 comm_point_callback_type* callback, void* callback_arg);
466 * Create commpoint to listen to a local domain pipe descriptor.
467 * @param base: in which base to alloc the commpoint.
468 * @param fd: file descriptor.
469 * @param writing: true if you want to listen to writes, false for reads.
470 * @param callback: callback function pointer for the handler.
471 * @param callback_arg: will be passed to your callback function.
472 * @return: the commpoint or NULL on error.
474 struct comm_point* comm_point_create_raw(struct comm_base* base,
476 comm_point_callback_type* callback, void* callback_arg);
479 * Close a comm point fd.
480 * @param c: comm point to close.
482 void comm_point_close(struct comm_point* c);
485 * Close and deallocate (free) the comm point. If the comm point is
486 * a tcp-accept point, also its tcp-handler points are deleted.
487 * @param c: comm point to delete.
489 void comm_point_delete(struct comm_point* c);
492 * Send reply. Put message into commpoint buffer.
493 * @param repinfo: The reply info copied from a commpoint callback call.
495 void comm_point_send_reply(struct comm_reply* repinfo);
498 * Drop reply. Cleans up.
499 * @param repinfo: The reply info copied from a commpoint callback call.
501 void comm_point_drop_reply(struct comm_reply* repinfo);
504 * Send an udp message over a commpoint.
505 * @param c: commpoint to send it from.
506 * @param packet: what to send.
507 * @param addr: where to send it to.
508 * @param addrlen: length of addr.
509 * @return: false on a failure.
511 int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
512 struct sockaddr* addr, socklen_t addrlen);
515 * Stop listening for input on the commpoint. No callbacks will happen.
516 * @param c: commpoint to disable. The fd is not closed.
518 void comm_point_stop_listening(struct comm_point* c);
521 * Start listening again for input on the comm point.
522 * @param c: commpoint to enable again.
523 * @param newfd: new fd, or -1 to leave fd be.
524 * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout.
527 void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
530 * Stop listening and start listening again for reading or writing.
531 * @param c: commpoint
532 * @param rd: if true, listens for reading.
533 * @param wr: if true, listens for writing.
535 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
538 * Get size of memory used by comm point.
539 * For TCP handlers this includes subhandlers.
540 * For UDP handlers, this does not include the (shared) UDP buffer.
541 * @param c: commpoint.
542 * @return size in bytes.
544 size_t comm_point_get_mem(struct comm_point* c);
547 * create timer. Not active upon creation.
548 * @param base: event handling base.
549 * @param cb: callback function: void myfunc(void* myarg);
550 * @param cb_arg: user callback argument.
551 * @return: the new timer or NULL on error.
553 struct comm_timer* comm_timer_create(struct comm_base* base,
554 void (*cb)(void*), void* cb_arg);
557 * disable timer. Stops callbacks from happening.
558 * @param timer: to disable.
560 void comm_timer_disable(struct comm_timer* timer);
563 * reset timevalue for timer.
564 * @param timer: timer to (re)set.
565 * @param tv: when the timer should activate. if NULL timer is disabled.
567 void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
571 * @param timer: to delete.
573 void comm_timer_delete(struct comm_timer* timer);
576 * see if timeout has been set to a value.
577 * @param timer: the timer to examine.
578 * @return: false if disabled or not set.
580 int comm_timer_is_set(struct comm_timer* timer);
583 * Get size of memory used by comm timer.
584 * @param timer: the timer to examine.
585 * @return size in bytes.
587 size_t comm_timer_get_mem(struct comm_timer* timer);
590 * Create a signal handler. Call signal_bind() later to bind to a signal.
591 * @param base: communication base to use.
592 * @param callback: called when signal is caught.
593 * @param cb_arg: user argument to callback
594 * @return: the signal struct or NULL on error.
596 struct comm_signal* comm_signal_create(struct comm_base* base,
597 void (*callback)(int, void*), void* cb_arg);
600 * Bind signal struct to catch a signal. A signle comm_signal can be bound
601 * to multiple signals, calling comm_signal_bind multiple times.
602 * @param comsig: the communication point, with callback information.
603 * @param sig: signal number.
604 * @return: true on success. false on error.
606 int comm_signal_bind(struct comm_signal* comsig, int sig);
609 * Delete the signal communication point.
610 * @param comsig: to delete.
612 void comm_signal_delete(struct comm_signal* comsig);
615 * perform accept(2) with error checking.
616 * @param c: commpoint with accept fd.
617 * @param addr: remote end returned here.
618 * @param addrlen: length of remote end returned here.
619 * @return new fd, or -1 on error.
620 * if -1, error message has been printed if necessary, simply drop
621 * out of the reading handler.
623 int comm_point_perform_accept(struct comm_point* c,
624 struct sockaddr_storage* addr, socklen_t* addrlen);
626 /**** internal routines ****/
629 * This routine is published for checks and tests, and is only used internally.
630 * handle libevent callback for udp comm point.
631 * @param fd: file descriptor.
632 * @param event: event bits from libevent:
633 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
634 * @param arg: the comm_point structure.
636 void comm_point_udp_callback(int fd, short event, void* arg);
639 * This routine is published for checks and tests, and is only used internally.
640 * handle libevent callback for udp ancillary data comm point.
641 * @param fd: file descriptor.
642 * @param event: event bits from libevent:
643 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
644 * @param arg: the comm_point structure.
646 void comm_point_udp_ancil_callback(int fd, short event, void* arg);
649 * This routine is published for checks and tests, and is only used internally.
650 * handle libevent callback for tcp accept comm point
651 * @param fd: file descriptor.
652 * @param event: event bits from libevent:
653 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
654 * @param arg: the comm_point structure.
656 void comm_point_tcp_accept_callback(int fd, short event, void* arg);
659 * This routine is published for checks and tests, and is only used internally.
660 * handle libevent callback for tcp data comm point
661 * @param fd: file descriptor.
662 * @param event: event bits from libevent:
663 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
664 * @param arg: the comm_point structure.
666 void comm_point_tcp_handle_callback(int fd, short event, void* arg);
669 * This routine is published for checks and tests, and is only used internally.
670 * handle libevent callback for timer comm.
671 * @param fd: file descriptor (always -1).
672 * @param event: event bits from libevent:
673 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
674 * @param arg: the comm_timer structure.
676 void comm_timer_callback(int fd, short event, void* arg);
679 * This routine is published for checks and tests, and is only used internally.
680 * handle libevent callback for signal comm.
681 * @param fd: file descriptor (used for the signal number).
682 * @param event: event bits from libevent:
683 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
684 * @param arg: the internal commsignal structure.
686 void comm_signal_callback(int fd, short event, void* arg);
689 * This routine is published for checks and tests, and is only used internally.
690 * libevent callback for AF_UNIX fds
691 * @param fd: file descriptor.
692 * @param event: event bits from libevent:
693 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
694 * @param arg: the comm_point structure.
696 void comm_point_local_handle_callback(int fd, short event, void* arg);
699 * This routine is published for checks and tests, and is only used internally.
700 * libevent callback for raw fd access.
701 * @param fd: file descriptor.
702 * @param event: event bits from libevent:
703 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
704 * @param arg: the comm_point structure.
706 void comm_point_raw_handle_callback(int fd, short event, void* arg);
709 * This routine is published for checks and tests, and is only used internally.
710 * libevent callback for timeout on slow accept.
711 * @param fd: file descriptor.
712 * @param event: event bits from libevent:
713 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
714 * @param arg: the comm_point structure.
716 void comm_base_handle_slow_accept(int fd, short event, void* arg);
720 * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
721 * the winsock_event of this for proper TCP nonblocking implementation.
722 * @param c: comm_point, fd must be set its struct event is registered.
723 * @param ssl: openssl SSL, fd must be set so it has a bio.
725 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
728 /** see if errno for tcp connect has to be logged or not. This uses errno */
729 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
731 #endif /* NET_EVENT_H */