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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * 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 <ldns/buffer.h>
68 /* internal event notification data storage structure. */
69 struct internal_event;
71 struct internal_timer;
73 /** callback from communication point function type */
74 typedef int comm_point_callback_t(struct comm_point*, void*, int,
77 /** to pass no_error to callback function */
78 #define NETEVENT_NOERROR 0
79 /** to pass closed connection to callback function */
80 #define NETEVENT_CLOSED -1
81 /** to pass timeout happened to callback function */
82 #define NETEVENT_TIMEOUT -2
83 /** to pass fallback from capsforID to callback function; 0x20 failed */
84 #define NETEVENT_CAPSFAIL -3
86 /** timeout to slow accept calls when not possible, in msec. */
87 #define NETEVENT_SLOW_ACCEPT_TIME 2000
90 * A communication point dispatcher. Thread specific.
93 /** behind the scenes structure. with say libevent info. alloced */
94 struct internal_base* eb;
95 /** callback to stop listening on accept sockets,
96 * performed when accept() will not function properly */
97 void (*stop_accept)(void*);
98 /** callback to start listening on accept sockets, performed
99 * after stop_accept() then a timeout has passed. */
100 void (*start_accept)(void*);
101 /** user argument for stop_accept and start_accept functions */
106 * Reply information for a communication point.
109 /** the comm_point with fd to send reply on to. */
110 struct comm_point* c;
111 /** the address (for UDP based communication) */
112 struct sockaddr_storage addr;
113 /** length of address */
115 /** return type 0 (none), 4(IP4), 6(IP6) */
117 /** the return source interface data */
120 struct in6_pktinfo v6info;
123 struct in_pktinfo v4info;
124 #elif defined(IP_RECVDSTADDR)
125 struct in_addr v4addr;
128 /** variable with return source data */
133 * Communication point to the network
134 * These behaviours can be accomplished by setting the flags
135 * and passing return values from the callback.
136 * udp frontside: called after readdone. sendafter.
137 * tcp frontside: called readdone, sendafter. close.
138 * udp behind: called after readdone. No send after.
139 * tcp behind: write done, read done, then called. No send after.
142 /** behind the scenes structure, with say libevent info. alloced. */
143 struct internal_event* ev;
145 /** file descriptor for communication point */
148 /** timeout (NULL if it does not). Malloced. */
149 struct timeval* timeout;
151 /** buffer pointer. Either to perthread, or own buffer or NULL */
154 /* -------- TCP Handler -------- */
155 /** Read/Write state for TCP */
157 /** The current read/write count for TCP */
158 size_t tcp_byte_count;
159 /** parent communication point (for TCP sockets) */
160 struct comm_point* tcp_parent;
161 /** sockaddr from peer, for TCP handlers */
162 struct comm_reply repinfo;
164 /* -------- TCP Accept -------- */
165 /** the number of TCP handlers for this tcp-accept socket */
167 /** malloced array of tcp handlers for a tcp-accept,
168 of size max_tcp_count. */
169 struct comm_point** tcp_handlers;
170 /** linked list of free tcp_handlers to use for new queries.
171 For tcp_accept the first entry, for tcp_handlers the next one. */
172 struct comm_point* tcp_free;
174 /* -------- SSL TCP DNS ------- */
175 /** the SSL object with rw bio (owned) or for commaccept ctx ref */
177 /** handshake state for init and renegotiate */
179 /** no handshake, it has been done */
180 comm_ssl_shake_none = 0,
181 /** ssl initial handshake wants to read */
183 /** ssl initial handshake wants to write */
184 comm_ssl_shake_write,
185 /** ssl_write wants to read */
186 comm_ssl_shake_hs_read,
187 /** ssl_read wants to write */
188 comm_ssl_shake_hs_write
191 /** is this a UDP, TCP-accept or TCP socket. */
192 enum comm_point_type {
193 /** UDP socket - handle datagrams. */
195 /** TCP accept socket - only creates handlers if readable. */
197 /** TCP handler socket - handle byteperbyte readwrite. */
199 /** AF_UNIX socket - for internal commands. */
201 /** raw - not DNS format - for pipe readers and writers */
204 /** variable with type of socket, UDP,TCP-accept,TCP,pipe */
207 /* ---------- Behaviour ----------- */
208 /** if set the connection is NOT closed on delete. */
211 /** if set, the connection is closed on error, on timeout,
212 and after read/write completes. No callback is done. */
215 /** if set, read/write completes:
216 read/write state of tcp is toggled.
217 buffer reset/bytecount reset.
219 So that when that is done the callback is called. */
220 int tcp_do_toggle_rw;
222 /** if set, checks for pending error from nonblocking connect() call.*/
223 int tcp_check_nb_connect;
225 /** number of queries outstanding on this socket, used by
226 * outside network for udp ports */
229 /** callback when done.
230 tcp_accept does not get called back, is NULL then.
231 If a timeout happens, callback with timeout=1 is called.
232 If an error happens, callback is called with error set
233 nonzero. If not NETEVENT_NOERROR, it is an errno value.
234 If the connection is closed (by remote end) then the
235 callback is called with error set to NETEVENT_CLOSED=-1.
236 If a timeout happens on the connection, the error is set to
238 The reply_info can be copied if the reply needs to happen at a
239 later time. It consists of a struct with commpoint and address.
240 It can be passed to a msg send routine some time later.
241 Note the reply information is temporary and must be copied.
242 NULL is passed for_reply info, in cases where error happened.
245 int my_callback(struct comm_point* c, void* my_arg, int error,
246 struct comm_reply *reply_info);
248 if the routine returns 0, nothing is done.
249 Notzero, the buffer will be sent back to client.
250 For UDP this is done without changing the commpoint.
251 In TCP it sets write state.
253 comm_point_callback_t* callback;
254 /** argument to pass to callback. */
259 * Structure only for making timeout events.
262 /** the internal event stuff */
263 struct internal_timer* ev_timer;
265 /** callback function, takes user arg only */
266 void (*callback)(void*);
268 /** callback user argument */
273 * Structure only for signal events.
276 /** the communication base */
277 struct comm_base* base;
279 /** the internal event stuff */
280 struct internal_signal* ev_signal;
282 /** callback function, takes signal number and user arg */
283 void (*callback)(int, void*);
285 /** callback user argument */
290 * Create a new comm base.
291 * @param sigs: if true it attempts to create a default loop for
293 * @return: the new comm base. NULL on error.
295 struct comm_base* comm_base_create(int sigs);
298 * Destroy a comm base.
299 * All comm points must have been deleted.
300 * @param b: the base to delete.
302 void comm_base_delete(struct comm_base* b);
305 * Obtain two pointers. The pointers never change (until base_delete()).
306 * The pointers point to time values that are updated regularly.
307 * @param b: the communication base that will update the time values.
308 * @param tt: pointer to time in seconds is returned.
309 * @param tv: pointer to time in microseconds is returned.
311 void comm_base_timept(struct comm_base* b, uint32_t** tt, struct timeval** tv);
314 * Dispatch the comm base events.
315 * @param b: the communication to perform.
317 void comm_base_dispatch(struct comm_base* b);
320 * Exit from dispatch loop.
321 * @param b: the communication base that is in dispatch().
323 void comm_base_exit(struct comm_base* b);
326 * Set the slow_accept mode handlers. You can not provide these if you do
327 * not perform accept() calls.
328 * @param b: comm base
329 * @param stop_accept: function that stops listening to accept fds.
330 * @param start_accept: function that resumes listening to accept fds.
331 * @param arg: callback arg to pass to the functions.
333 void comm_base_set_slow_accept_handlers(struct comm_base* b,
334 void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
337 * Access internal data structure (for util/tube.c on windows)
338 * @param b: comm base
339 * @return event_base. Could be libevent, or internal event handler.
341 struct event_base* comm_base_internal(struct comm_base* b);
344 * Create an UDP comm point. Calls malloc.
345 * setups the structure with the parameters you provide.
346 * @param base: in which base to alloc the commpoint.
347 * @param fd : file descriptor of open UDP socket.
348 * @param buffer: shared buffer by UDP sockets from this thread.
349 * @param callback: callback function pointer.
350 * @param callback_arg: will be passed to your callback function.
351 * @return: returns the allocated communication point. NULL on error.
352 * Sets timeout to NULL. Turns off TCP options.
354 struct comm_point* comm_point_create_udp(struct comm_base* base,
355 int fd, ldns_buffer* buffer,
356 comm_point_callback_t* callback, void* callback_arg);
359 * Create an UDP with ancillary data comm point. Calls malloc.
360 * Uses recvmsg instead of recv to get udp message.
361 * setups the structure with the parameters you provide.
362 * @param base: in which base to alloc the commpoint.
363 * @param fd : file descriptor of open UDP socket.
364 * @param buffer: shared buffer by UDP sockets from this thread.
365 * @param callback: callback function pointer.
366 * @param callback_arg: will be passed to your callback function.
367 * @return: returns the allocated communication point. NULL on error.
368 * Sets timeout to NULL. Turns off TCP options.
370 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
371 int fd, ldns_buffer* buffer,
372 comm_point_callback_t* callback, void* callback_arg);
375 * Create a TCP listener comm point. Calls malloc.
376 * Setups the structure with the parameters you provide.
377 * Also Creates TCP Handlers, pre allocated for you.
378 * Uses the parameters you provide.
379 * @param base: in which base to alloc the commpoint.
380 * @param fd: file descriptor of open TCP socket set to listen nonblocking.
381 * @param num: becomes max_tcp_count, the routine allocates that
382 * many tcp handler commpoints.
383 * @param bufsize: size of buffer to create for handlers.
384 * @param callback: callback function pointer for TCP handlers.
385 * @param callback_arg: will be passed to your callback function.
386 * @return: returns the TCP listener commpoint. You can find the
387 * TCP handlers in the array inside the listener commpoint.
388 * returns NULL on error.
389 * Inits timeout to NULL. All handlers are on the free list.
391 struct comm_point* comm_point_create_tcp(struct comm_base* base,
392 int fd, int num, size_t bufsize,
393 comm_point_callback_t* callback, void* callback_arg);
396 * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
397 * @param base: in which base to alloc the commpoint.
398 * @param bufsize: size of buffer to create for handlers.
399 * @param callback: callback function pointer for the handler.
400 * @param callback_arg: will be passed to your callback function.
401 * @return: the commpoint or NULL on error.
403 struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
404 size_t bufsize, comm_point_callback_t* callback, void* callback_arg);
407 * Create commpoint to listen to a local domain file descriptor.
408 * @param base: in which base to alloc the commpoint.
409 * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
410 * @param bufsize: size of buffer to create for handlers.
411 * @param callback: callback function pointer for the handler.
412 * @param callback_arg: will be passed to your callback function.
413 * @return: the commpoint or NULL on error.
415 struct comm_point* comm_point_create_local(struct comm_base* base,
416 int fd, size_t bufsize,
417 comm_point_callback_t* callback, void* callback_arg);
420 * Create commpoint to listen to a local domain pipe descriptor.
421 * @param base: in which base to alloc the commpoint.
422 * @param fd: file descriptor.
423 * @param writing: true if you want to listen to writes, false for reads.
424 * @param callback: callback function pointer for the handler.
425 * @param callback_arg: will be passed to your callback function.
426 * @return: the commpoint or NULL on error.
428 struct comm_point* comm_point_create_raw(struct comm_base* base,
430 comm_point_callback_t* callback, void* callback_arg);
433 * Close a comm point fd.
434 * @param c: comm point to close.
436 void comm_point_close(struct comm_point* c);
439 * Close and deallocate (free) the comm point. If the comm point is
440 * a tcp-accept point, also its tcp-handler points are deleted.
441 * @param c: comm point to delete.
443 void comm_point_delete(struct comm_point* c);
446 * Send reply. Put message into commpoint buffer.
447 * @param repinfo: The reply info copied from a commpoint callback call.
449 void comm_point_send_reply(struct comm_reply* repinfo);
452 * Drop reply. Cleans up.
453 * @param repinfo: The reply info copied from a commpoint callback call.
455 void comm_point_drop_reply(struct comm_reply* repinfo);
458 * Send an udp message over a commpoint.
459 * @param c: commpoint to send it from.
460 * @param packet: what to send.
461 * @param addr: where to send it to.
462 * @param addrlen: length of addr.
463 * @return: false on a failure.
465 int comm_point_send_udp_msg(struct comm_point* c, ldns_buffer* packet,
466 struct sockaddr* addr, socklen_t addrlen);
469 * Stop listening for input on the commpoint. No callbacks will happen.
470 * @param c: commpoint to disable. The fd is not closed.
472 void comm_point_stop_listening(struct comm_point* c);
475 * Start listening again for input on the comm point.
476 * @param c: commpoint to enable again.
477 * @param newfd: new fd, or -1 to leave fd be.
478 * @param sec: timeout in seconds, or -1 for no (change to the) timeout.
480 void comm_point_start_listening(struct comm_point* c, int newfd, int sec);
483 * Stop listening and start listening again for reading or writing.
484 * @param c: commpoint
485 * @param rd: if true, listens for reading.
486 * @param wr: if true, listens for writing.
488 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
491 * Get size of memory used by comm point.
492 * For TCP handlers this includes subhandlers.
493 * For UDP handlers, this does not include the (shared) UDP buffer.
494 * @param c: commpoint.
495 * @return size in bytes.
497 size_t comm_point_get_mem(struct comm_point* c);
500 * create timer. Not active upon creation.
501 * @param base: event handling base.
502 * @param cb: callback function: void myfunc(void* myarg);
503 * @param cb_arg: user callback argument.
504 * @return: the new timer or NULL on error.
506 struct comm_timer* comm_timer_create(struct comm_base* base,
507 void (*cb)(void*), void* cb_arg);
510 * disable timer. Stops callbacks from happening.
511 * @param timer: to disable.
513 void comm_timer_disable(struct comm_timer* timer);
516 * reset timevalue for timer.
517 * @param timer: timer to (re)set.
518 * @param tv: when the timer should activate. if NULL timer is disabled.
520 void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
524 * @param timer: to delete.
526 void comm_timer_delete(struct comm_timer* timer);
529 * see if timeout has been set to a value.
530 * @param timer: the timer to examine.
531 * @return: false if disabled or not set.
533 int comm_timer_is_set(struct comm_timer* timer);
536 * Get size of memory used by comm timer.
537 * @param timer: the timer to examine.
538 * @return size in bytes.
540 size_t comm_timer_get_mem(struct comm_timer* timer);
543 * Create a signal handler. Call signal_bind() later to bind to a signal.
544 * @param base: communication base to use.
545 * @param callback: called when signal is caught.
546 * @param cb_arg: user argument to callback
547 * @return: the signal struct or NULL on error.
549 struct comm_signal* comm_signal_create(struct comm_base* base,
550 void (*callback)(int, void*), void* cb_arg);
553 * Bind signal struct to catch a signal. A signle comm_signal can be bound
554 * to multiple signals, calling comm_signal_bind multiple times.
555 * @param comsig: the communication point, with callback information.
556 * @param sig: signal number.
557 * @return: true on success. false on error.
559 int comm_signal_bind(struct comm_signal* comsig, int sig);
562 * Delete the signal communication point.
563 * @param comsig: to delete.
565 void comm_signal_delete(struct comm_signal* comsig);
568 * perform accept(2) with error checking.
569 * @param c: commpoint with accept fd.
570 * @param addr: remote end returned here.
571 * @param addrlen: length of remote end returned here.
572 * @return new fd, or -1 on error.
573 * if -1, error message has been printed if necessary, simply drop
574 * out of the reading handler.
576 int comm_point_perform_accept(struct comm_point* c,
577 struct sockaddr_storage* addr, socklen_t* addrlen);
579 /**** internal routines ****/
582 * This routine is published for checks and tests, and is only used internally.
583 * handle libevent callback for udp comm point.
584 * @param fd: file descriptor.
585 * @param event: event bits from libevent:
586 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
587 * @param arg: the comm_point structure.
589 void comm_point_udp_callback(int fd, short event, void* arg);
592 * This routine is published for checks and tests, and is only used internally.
593 * handle libevent callback for udp ancillary data comm point.
594 * @param fd: file descriptor.
595 * @param event: event bits from libevent:
596 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
597 * @param arg: the comm_point structure.
599 void comm_point_udp_ancil_callback(int fd, short event, void* arg);
602 * This routine is published for checks and tests, and is only used internally.
603 * handle libevent callback for tcp accept comm point
604 * @param fd: file descriptor.
605 * @param event: event bits from libevent:
606 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
607 * @param arg: the comm_point structure.
609 void comm_point_tcp_accept_callback(int fd, short event, void* arg);
612 * This routine is published for checks and tests, and is only used internally.
613 * handle libevent callback for tcp data comm point
614 * @param fd: file descriptor.
615 * @param event: event bits from libevent:
616 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
617 * @param arg: the comm_point structure.
619 void comm_point_tcp_handle_callback(int fd, short event, void* arg);
622 * This routine is published for checks and tests, and is only used internally.
623 * handle libevent callback for timer comm.
624 * @param fd: file descriptor (always -1).
625 * @param event: event bits from libevent:
626 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
627 * @param arg: the comm_timer structure.
629 void comm_timer_callback(int fd, short event, void* arg);
632 * This routine is published for checks and tests, and is only used internally.
633 * handle libevent callback for signal comm.
634 * @param fd: file descriptor (used for the signal number).
635 * @param event: event bits from libevent:
636 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
637 * @param arg: the internal commsignal structure.
639 void comm_signal_callback(int fd, short event, void* arg);
642 * This routine is published for checks and tests, and is only used internally.
643 * libevent callback for AF_UNIX fds
644 * @param fd: file descriptor.
645 * @param event: event bits from libevent:
646 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
647 * @param arg: the comm_point structure.
649 void comm_point_local_handle_callback(int fd, short event, void* arg);
652 * This routine is published for checks and tests, and is only used internally.
653 * libevent callback for raw fd access.
654 * @param fd: file descriptor.
655 * @param event: event bits from libevent:
656 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
657 * @param arg: the comm_point structure.
659 void comm_point_raw_handle_callback(int fd, short event, void* arg);
662 * This routine is published for checks and tests, and is only used internally.
663 * libevent callback for timeout on slow accept.
664 * @param fd: file descriptor.
665 * @param event: event bits from libevent:
666 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
667 * @param arg: the comm_point structure.
669 void comm_base_handle_slow_accept(int fd, short event, void* arg);
673 * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
674 * the winsock_event of this for proper TCP nonblocking implementation.
675 * @param c: comm_point, fd must be set its struct event is registered.
676 * @param ssl: openssl SSL, fd must be set so it has a bio.
678 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
681 /** see if errno for tcp connect has to be logged or not. This uses errno */
682 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
684 #endif /* NET_EVENT_H */