xref: /openbsd-src/usr.sbin/unbound/util/ub_event.h (revision bdfc4d552272a291ee133ad4966bcce75b5a64cb)
12ee382b6Ssthen /*
22ee382b6Ssthen  * util/ub_event.h - indirection layer for pluggable events
32ee382b6Ssthen  *
42ee382b6Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
52ee382b6Ssthen  *
62ee382b6Ssthen  * This software is open source.
72ee382b6Ssthen  *
82ee382b6Ssthen  * Redistribution and use in source and binary forms, with or without
92ee382b6Ssthen  * modification, are permitted provided that the following conditions
102ee382b6Ssthen  * are met:
112ee382b6Ssthen  *
122ee382b6Ssthen  * Redistributions of source code must retain the above copyright notice,
132ee382b6Ssthen  * this list of conditions and the following disclaimer.
142ee382b6Ssthen  *
152ee382b6Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
162ee382b6Ssthen  * this list of conditions and the following disclaimer in the documentation
172ee382b6Ssthen  * and/or other materials provided with the distribution.
182ee382b6Ssthen  *
192ee382b6Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
202ee382b6Ssthen  * be used to endorse or promote products derived from this software without
212ee382b6Ssthen  * specific prior written permission.
222ee382b6Ssthen  *
232ee382b6Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
242ee382b6Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
252ee382b6Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
262ee382b6Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
272ee382b6Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
282ee382b6Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
292ee382b6Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
302ee382b6Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
312ee382b6Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
322ee382b6Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
332ee382b6Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342ee382b6Ssthen  */
352ee382b6Ssthen 
362ee382b6Ssthen /**
372ee382b6Ssthen  * \file
382ee382b6Ssthen  *
392ee382b6Ssthen  * This file contains prototypes for event loop functions.
402ee382b6Ssthen  *
412ee382b6Ssthen  */
422ee382b6Ssthen 
432ee382b6Ssthen #ifndef UB_EVENT_H
442ee382b6Ssthen #define UB_EVENT_H
452ee382b6Ssthen 
462ee382b6Ssthen struct ub_event_base;
472ee382b6Ssthen struct ub_event;
482ee382b6Ssthen struct comm_base;
492ee382b6Ssthen struct event_base;
502ee382b6Ssthen 
512ee382b6Ssthen /** event timeout */
522ee382b6Ssthen #define UB_EV_TIMEOUT      0x01
532ee382b6Ssthen /** event fd readable */
542ee382b6Ssthen #define UB_EV_READ         0x02
552ee382b6Ssthen /** event fd writable */
562ee382b6Ssthen #define UB_EV_WRITE        0x04
572ee382b6Ssthen /** event signal */
582ee382b6Ssthen #define UB_EV_SIGNAL       0x08
592ee382b6Ssthen /** event must persist */
602ee382b6Ssthen #define UB_EV_PERSIST      0x10
612ee382b6Ssthen 
622ee382b6Ssthen /** Returns event-base type. Could be "mini-event", "winsock-event" for the
632ee382b6Ssthen  * daemon compile, and will be "pluggable-event<PACKAGE_VERSION>" for
642ee382b6Ssthen  * libunbound.
652ee382b6Ssthen  */
6677079be7Ssthen const char* ub_event_get_version(void);
672ee382b6Ssthen /** Return the name, system and method for the pluggable event base */
682ee382b6Ssthen void ub_get_event_sys(struct ub_event_base*, const char** n, const char** s,
692ee382b6Ssthen 	const char** m);
70*bdfc4d55Sflorian /** Return a default event base. In the daemon this will be the only event
712ee382b6Ssthen  * bases used.
722ee382b6Ssthen  */
732ee382b6Ssthen struct ub_event_base* ub_default_event_base(int, time_t*, struct timeval*);
742ee382b6Ssthen /** Return an ub_event_base constructed for the given libevent event base */
752ee382b6Ssthen struct ub_event_base* ub_libevent_event_base(struct event_base*);
762ee382b6Ssthen /** Return the libevent base underlying the given ub_event_base.  Will return
772ee382b6Ssthen  * NULL when the ub_event_base does not have an underlying libevent event base
782ee382b6Ssthen  */
792ee382b6Ssthen struct event_base* ub_libevent_get_event_base(struct ub_event_base*);
802ee382b6Ssthen /** Free event base. Free events yourself */
812ee382b6Ssthen void ub_event_base_free(struct ub_event_base*);
822ee382b6Ssthen /** Run the event base */
832ee382b6Ssthen int ub_event_base_dispatch(struct ub_event_base*);
842ee382b6Ssthen /** exit that loop */
852ee382b6Ssthen int ub_event_base_loopexit(struct ub_event_base*);
862ee382b6Ssthen 
872ee382b6Ssthen /** Create a new ub_event for the event base */
882ee382b6Ssthen struct ub_event* ub_event_new(struct ub_event_base*,
892ee382b6Ssthen 	int fd, short bits, void (*cb)(int, short, void*), void* arg);
902ee382b6Ssthen /** Create a new ub_event signal for the event base */
912ee382b6Ssthen struct ub_event* ub_signal_new(struct ub_event_base*, int fd,
922ee382b6Ssthen 	void (*cb)(int, short, void*), void* arg);
932ee382b6Ssthen /** Create a new ub_event associated with the wsaevent for the event base */
942ee382b6Ssthen struct ub_event* ub_winsock_register_wsaevent(struct ub_event_base*,
952ee382b6Ssthen 	void* wsaevent, void (*cb)(int, short, void*), void* arg);
962ee382b6Ssthen 
972ee382b6Ssthen /** Add event bits for this event to fire on */
982ee382b6Ssthen void ub_event_add_bits(struct ub_event*, short bits);
992ee382b6Ssthen  /** Configure the event so it will not longer fire on given bits */
1002ee382b6Ssthen void ub_event_del_bits(struct ub_event*, short bits);
1012ee382b6Ssthen /** Change or set the file descriptor on the event */
1022ee382b6Ssthen void ub_event_set_fd(struct ub_event*, int fd);
1032ee382b6Ssthen /** free the event */
1042ee382b6Ssthen void ub_event_free(struct ub_event*);
1052ee382b6Ssthen /** Activate the event.  The given timeval is an timeout value. */
1062ee382b6Ssthen int ub_event_add(struct ub_event*, struct timeval*);
1072ee382b6Ssthen /** Deactivate the event */
1082ee382b6Ssthen int ub_event_del(struct ub_event*);
1092ee382b6Ssthen /** Reconfigure and activate a timeout event */
1102ee382b6Ssthen int ub_timer_add(struct ub_event*, struct ub_event_base*,
1112ee382b6Ssthen 	void (*cb)(int, short, void*), void* arg, struct timeval*);
1122ee382b6Ssthen /** Deactivate the timeout event */
1132ee382b6Ssthen int ub_timer_del(struct ub_event*);
1142ee382b6Ssthen /** Activate a signal event */
1152ee382b6Ssthen int ub_signal_add(struct ub_event*, struct timeval*);
1162ee382b6Ssthen /** Deactivate a signal event */
1172ee382b6Ssthen int ub_signal_del(struct ub_event*);
1182ee382b6Ssthen /** Free a with a wsaevent associated event */
1192ee382b6Ssthen void ub_winsock_unregister_wsaevent(struct ub_event* ev);
1202ee382b6Ssthen /** Signal the eventloop when a TCP windows socket will block on next read
1212ee382b6Ssthen  * or write (given by the eventbits)
1222ee382b6Ssthen  */
1232ee382b6Ssthen void ub_winsock_tcp_wouldblock(struct ub_event*, int bits);
1242ee382b6Ssthen /** Equip the comm_base with the current time */
1252ee382b6Ssthen void ub_comm_base_now(struct comm_base* cb);
1262ee382b6Ssthen 
1272ee382b6Ssthen #endif /* UB_EVENT_H */
128