]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/util/netevent.h
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.
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 */
152 struct sldns_buffer
* buffer
;
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 /* -------- dnstap ------- */
192 /** the dnstap environment */
193 struct dt_env
* dtenv
;
195 /** is this a UDP, TCP-accept or TCP socket. */
196 enum comm_point_type
{
197 /** UDP socket - handle datagrams. */
199 /** TCP accept socket - only creates handlers if readable. */
201 /** TCP handler socket - handle byteperbyte readwrite. */
203 /** AF_UNIX socket - for internal commands. */
205 /** raw - not DNS format - for pipe readers and writers */
208 /** variable with type of socket, UDP,TCP-accept,TCP,pipe */
211 /* ---------- Behaviour ----------- */
212 /** if set the connection is NOT closed on delete. */
215 /** if set, the connection is closed on error, on timeout,
216 and after read/write completes. No callback is done. */
219 /** if set, read/write completes:
220 read/write state of tcp is toggled.
221 buffer reset/bytecount reset.
223 So that when that is done the callback is called. */
224 int tcp_do_toggle_rw
;
226 /** if set, checks for pending error from nonblocking connect() call.*/
227 int tcp_check_nb_connect
;
229 /** number of queries outstanding on this socket, used by
230 * outside network for udp ports */
233 /** callback when done.
234 tcp_accept does not get called back, is NULL then.
235 If a timeout happens, callback with timeout=1 is called.
236 If an error happens, callback is called with error set
237 nonzero. If not NETEVENT_NOERROR, it is an errno value.
238 If the connection is closed (by remote end) then the
239 callback is called with error set to NETEVENT_CLOSED=-1.
240 If a timeout happens on the connection, the error is set to
242 The reply_info can be copied if the reply needs to happen at a
243 later time. It consists of a struct with commpoint and address.
244 It can be passed to a msg send routine some time later.
245 Note the reply information is temporary and must be copied.
246 NULL is passed for_reply info, in cases where error happened.
249 int my_callback(struct comm_point* c, void* my_arg, int error,
250 struct comm_reply *reply_info);
252 if the routine returns 0, nothing is done.
253 Notzero, the buffer will be sent back to client.
254 For UDP this is done without changing the commpoint.
255 In TCP it sets write state.
257 comm_point_callback_t
* callback
;
258 /** argument to pass to callback. */
263 * Structure only for making timeout events.
266 /** the internal event stuff */
267 struct internal_timer
* ev_timer
;
269 /** callback function, takes user arg only */
270 void (*callback
)(void*);
272 /** callback user argument */
277 * Structure only for signal events.
280 /** the communication base */
281 struct comm_base
* base
;
283 /** the internal event stuff */
284 struct internal_signal
* ev_signal
;
286 /** callback function, takes signal number and user arg */
287 void (*callback
)(int, void*);
289 /** callback user argument */
294 * Create a new comm base.
295 * @param sigs: if true it attempts to create a default loop for
297 * @return: the new comm base. NULL on error.
299 struct comm_base
* comm_base_create(int sigs
);
302 * Create comm base that uses the given event_base (underlying event
303 * mechanism pointer).
304 * @param base: underlying lib event base.
305 * @return: the new comm base. NULL on error.
307 struct comm_base
* comm_base_create_event(struct event_base
* base
);
310 * Delete comm base structure but not the underlying lib event base.
311 * All comm points must have been deleted.
312 * @param b: the base to delete.
314 void comm_base_delete_no_base(struct comm_base
* b
);
317 * Destroy a comm base.
318 * All comm points must have been deleted.
319 * @param b: the base to delete.
321 void comm_base_delete(struct comm_base
* b
);
324 * Obtain two pointers. The pointers never change (until base_delete()).
325 * The pointers point to time values that are updated regularly.
326 * @param b: the communication base that will update the time values.
327 * @param tt: pointer to time in seconds is returned.
328 * @param tv: pointer to time in microseconds is returned.
330 void comm_base_timept(struct comm_base
* b
, time_t** tt
, struct timeval
** tv
);
333 * Dispatch the comm base events.
334 * @param b: the communication to perform.
336 void comm_base_dispatch(struct comm_base
* b
);
339 * Exit from dispatch loop.
340 * @param b: the communication base that is in dispatch().
342 void comm_base_exit(struct comm_base
* b
);
345 * Set the slow_accept mode handlers. You can not provide these if you do
346 * not perform accept() calls.
347 * @param b: comm base
348 * @param stop_accept: function that stops listening to accept fds.
349 * @param start_accept: function that resumes listening to accept fds.
350 * @param arg: callback arg to pass to the functions.
352 void comm_base_set_slow_accept_handlers(struct comm_base
* b
,
353 void (*stop_accept
)(void*), void (*start_accept
)(void*), void* arg
);
356 * Access internal data structure (for util/tube.c on windows)
357 * @param b: comm base
358 * @return event_base. Could be libevent, or internal event handler.
360 struct event_base
* comm_base_internal(struct comm_base
* b
);
363 * Create an UDP comm point. Calls malloc.
364 * setups the structure with the parameters you provide.
365 * @param base: in which base to alloc the commpoint.
366 * @param fd : file descriptor of open UDP socket.
367 * @param buffer: shared buffer by UDP sockets from this thread.
368 * @param callback: callback function pointer.
369 * @param callback_arg: will be passed to your callback function.
370 * @return: returns the allocated communication point. NULL on error.
371 * Sets timeout to NULL. Turns off TCP options.
373 struct comm_point
* comm_point_create_udp(struct comm_base
* base
,
374 int fd
, struct sldns_buffer
* buffer
,
375 comm_point_callback_t
* callback
, void* callback_arg
);
378 * Create an UDP with ancillary data comm point. Calls malloc.
379 * Uses recvmsg instead of recv to get udp message.
380 * setups the structure with the parameters you provide.
381 * @param base: in which base to alloc the commpoint.
382 * @param fd : file descriptor of open UDP socket.
383 * @param buffer: shared buffer by UDP sockets from this thread.
384 * @param callback: callback function pointer.
385 * @param callback_arg: will be passed to your callback function.
386 * @return: returns the allocated communication point. NULL on error.
387 * Sets timeout to NULL. Turns off TCP options.
389 struct comm_point
* comm_point_create_udp_ancil(struct comm_base
* base
,
390 int fd
, struct sldns_buffer
* buffer
,
391 comm_point_callback_t
* callback
, void* callback_arg
);
394 * Create a TCP listener comm point. Calls malloc.
395 * Setups the structure with the parameters you provide.
396 * Also Creates TCP Handlers, pre allocated for you.
397 * Uses the parameters you provide.
398 * @param base: in which base to alloc the commpoint.
399 * @param fd: file descriptor of open TCP socket set to listen nonblocking.
400 * @param num: becomes max_tcp_count, the routine allocates that
401 * many tcp handler commpoints.
402 * @param bufsize: size of buffer to create for handlers.
403 * @param callback: callback function pointer for TCP handlers.
404 * @param callback_arg: will be passed to your callback function.
405 * @return: returns the TCP listener commpoint. You can find the
406 * TCP handlers in the array inside the listener commpoint.
407 * returns NULL on error.
408 * Inits timeout to NULL. All handlers are on the free list.
410 struct comm_point
* comm_point_create_tcp(struct comm_base
* base
,
411 int fd
, int num
, size_t bufsize
,
412 comm_point_callback_t
* callback
, void* callback_arg
);
415 * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
416 * @param base: in which base to alloc the commpoint.
417 * @param bufsize: size of buffer to create for handlers.
418 * @param callback: callback function pointer for the handler.
419 * @param callback_arg: will be passed to your callback function.
420 * @return: the commpoint or NULL on error.
422 struct comm_point
* comm_point_create_tcp_out(struct comm_base
* base
,
423 size_t bufsize
, comm_point_callback_t
* callback
, void* callback_arg
);
426 * Create commpoint to listen to a local domain file descriptor.
427 * @param base: in which base to alloc the commpoint.
428 * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
429 * @param bufsize: size of buffer to create for handlers.
430 * @param callback: callback function pointer for the handler.
431 * @param callback_arg: will be passed to your callback function.
432 * @return: the commpoint or NULL on error.
434 struct comm_point
* comm_point_create_local(struct comm_base
* base
,
435 int fd
, size_t bufsize
,
436 comm_point_callback_t
* callback
, void* callback_arg
);
439 * Create commpoint to listen to a local domain pipe descriptor.
440 * @param base: in which base to alloc the commpoint.
441 * @param fd: file descriptor.
442 * @param writing: true if you want to listen to writes, false for reads.
443 * @param callback: callback function pointer for the handler.
444 * @param callback_arg: will be passed to your callback function.
445 * @return: the commpoint or NULL on error.
447 struct comm_point
* comm_point_create_raw(struct comm_base
* base
,
449 comm_point_callback_t
* callback
, void* callback_arg
);
452 * Close a comm point fd.
453 * @param c: comm point to close.
455 void comm_point_close(struct comm_point
* c
);
458 * Close and deallocate (free) the comm point. If the comm point is
459 * a tcp-accept point, also its tcp-handler points are deleted.
460 * @param c: comm point to delete.
462 void comm_point_delete(struct comm_point
* c
);
465 * Send reply. Put message into commpoint buffer.
466 * @param repinfo: The reply info copied from a commpoint callback call.
468 void comm_point_send_reply(struct comm_reply
* repinfo
);
471 * Drop reply. Cleans up.
472 * @param repinfo: The reply info copied from a commpoint callback call.
474 void comm_point_drop_reply(struct comm_reply
* repinfo
);
477 * Send an udp message over a commpoint.
478 * @param c: commpoint to send it from.
479 * @param packet: what to send.
480 * @param addr: where to send it to.
481 * @param addrlen: length of addr.
482 * @return: false on a failure.
484 int comm_point_send_udp_msg(struct comm_point
* c
, struct sldns_buffer
* packet
,
485 struct sockaddr
* addr
, socklen_t addrlen
);
488 * Stop listening for input on the commpoint. No callbacks will happen.
489 * @param c: commpoint to disable. The fd is not closed.
491 void comm_point_stop_listening(struct comm_point
* c
);
494 * Start listening again for input on the comm point.
495 * @param c: commpoint to enable again.
496 * @param newfd: new fd, or -1 to leave fd be.
497 * @param sec: timeout in seconds, or -1 for no (change to the) timeout.
499 void comm_point_start_listening(struct comm_point
* c
, int newfd
, int sec
);
502 * Stop listening and start listening again for reading or writing.
503 * @param c: commpoint
504 * @param rd: if true, listens for reading.
505 * @param wr: if true, listens for writing.
507 void comm_point_listen_for_rw(struct comm_point
* c
, int rd
, int wr
);
510 * Get size of memory used by comm point.
511 * For TCP handlers this includes subhandlers.
512 * For UDP handlers, this does not include the (shared) UDP buffer.
513 * @param c: commpoint.
514 * @return size in bytes.
516 size_t comm_point_get_mem(struct comm_point
* c
);
519 * create timer. Not active upon creation.
520 * @param base: event handling base.
521 * @param cb: callback function: void myfunc(void* myarg);
522 * @param cb_arg: user callback argument.
523 * @return: the new timer or NULL on error.
525 struct comm_timer
* comm_timer_create(struct comm_base
* base
,
526 void (*cb
)(void*), void* cb_arg
);
529 * disable timer. Stops callbacks from happening.
530 * @param timer: to disable.
532 void comm_timer_disable(struct comm_timer
* timer
);
535 * reset timevalue for timer.
536 * @param timer: timer to (re)set.
537 * @param tv: when the timer should activate. if NULL timer is disabled.
539 void comm_timer_set(struct comm_timer
* timer
, struct timeval
* tv
);
543 * @param timer: to delete.
545 void comm_timer_delete(struct comm_timer
* timer
);
548 * see if timeout has been set to a value.
549 * @param timer: the timer to examine.
550 * @return: false if disabled or not set.
552 int comm_timer_is_set(struct comm_timer
* timer
);
555 * Get size of memory used by comm timer.
556 * @param timer: the timer to examine.
557 * @return size in bytes.
559 size_t comm_timer_get_mem(struct comm_timer
* timer
);
562 * Create a signal handler. Call signal_bind() later to bind to a signal.
563 * @param base: communication base to use.
564 * @param callback: called when signal is caught.
565 * @param cb_arg: user argument to callback
566 * @return: the signal struct or NULL on error.
568 struct comm_signal
* comm_signal_create(struct comm_base
* base
,
569 void (*callback
)(int, void*), void* cb_arg
);
572 * Bind signal struct to catch a signal. A signle comm_signal can be bound
573 * to multiple signals, calling comm_signal_bind multiple times.
574 * @param comsig: the communication point, with callback information.
575 * @param sig: signal number.
576 * @return: true on success. false on error.
578 int comm_signal_bind(struct comm_signal
* comsig
, int sig
);
581 * Delete the signal communication point.
582 * @param comsig: to delete.
584 void comm_signal_delete(struct comm_signal
* comsig
);
587 * perform accept(2) with error checking.
588 * @param c: commpoint with accept fd.
589 * @param addr: remote end returned here.
590 * @param addrlen: length of remote end returned here.
591 * @return new fd, or -1 on error.
592 * if -1, error message has been printed if necessary, simply drop
593 * out of the reading handler.
595 int comm_point_perform_accept(struct comm_point
* c
,
596 struct sockaddr_storage
* addr
, socklen_t
* addrlen
);
598 /**** internal routines ****/
601 * This routine is published for checks and tests, and is only used internally.
602 * handle libevent callback for udp comm point.
603 * @param fd: file descriptor.
604 * @param event: event bits from libevent:
605 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
606 * @param arg: the comm_point structure.
608 void comm_point_udp_callback(int fd
, short event
, void* arg
);
611 * This routine is published for checks and tests, and is only used internally.
612 * handle libevent callback for udp ancillary data comm point.
613 * @param fd: file descriptor.
614 * @param event: event bits from libevent:
615 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
616 * @param arg: the comm_point structure.
618 void comm_point_udp_ancil_callback(int fd
, short event
, void* arg
);
621 * This routine is published for checks and tests, and is only used internally.
622 * handle libevent callback for tcp accept comm point
623 * @param fd: file descriptor.
624 * @param event: event bits from libevent:
625 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
626 * @param arg: the comm_point structure.
628 void comm_point_tcp_accept_callback(int fd
, short event
, void* arg
);
631 * This routine is published for checks and tests, and is only used internally.
632 * handle libevent callback for tcp data comm point
633 * @param fd: file descriptor.
634 * @param event: event bits from libevent:
635 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
636 * @param arg: the comm_point structure.
638 void comm_point_tcp_handle_callback(int fd
, short event
, void* arg
);
641 * This routine is published for checks and tests, and is only used internally.
642 * handle libevent callback for timer comm.
643 * @param fd: file descriptor (always -1).
644 * @param event: event bits from libevent:
645 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
646 * @param arg: the comm_timer structure.
648 void comm_timer_callback(int fd
, short event
, void* arg
);
651 * This routine is published for checks and tests, and is only used internally.
652 * handle libevent callback for signal comm.
653 * @param fd: file descriptor (used for the signal number).
654 * @param event: event bits from libevent:
655 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
656 * @param arg: the internal commsignal structure.
658 void comm_signal_callback(int fd
, short event
, void* arg
);
661 * This routine is published for checks and tests, and is only used internally.
662 * libevent callback for AF_UNIX fds
663 * @param fd: file descriptor.
664 * @param event: event bits from libevent:
665 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
666 * @param arg: the comm_point structure.
668 void comm_point_local_handle_callback(int fd
, short event
, void* arg
);
671 * This routine is published for checks and tests, and is only used internally.
672 * libevent callback for raw fd access.
673 * @param fd: file descriptor.
674 * @param event: event bits from libevent:
675 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
676 * @param arg: the comm_point structure.
678 void comm_point_raw_handle_callback(int fd
, short event
, void* arg
);
681 * This routine is published for checks and tests, and is only used internally.
682 * libevent callback for timeout on slow accept.
683 * @param fd: file descriptor.
684 * @param event: event bits from libevent:
685 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
686 * @param arg: the comm_point structure.
688 void comm_base_handle_slow_accept(int fd
, short event
, void* arg
);
692 * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
693 * the winsock_event of this for proper TCP nonblocking implementation.
694 * @param c: comm_point, fd must be set its struct event is registered.
695 * @param ssl: openssl SSL, fd must be set so it has a bio.
697 void comm_point_tcp_win_bio_cb(struct comm_point
* c
, void* ssl
);
700 /** see if errno for tcp connect has to be logged or not. This uses errno */
701 int tcp_connect_errno_needs_log(struct sockaddr
* addr
, socklen_t addrlen
);
703 #endif /* NET_EVENT_H */