1*ae8c6e27Sflorian /* 2*ae8c6e27Sflorian * util/ub_event.h - indirection layer for pluggable events 3*ae8c6e27Sflorian * 4*ae8c6e27Sflorian * Copyright (c) 2007, NLnet Labs. All rights reserved. 5*ae8c6e27Sflorian * 6*ae8c6e27Sflorian * This software is open source. 7*ae8c6e27Sflorian * 8*ae8c6e27Sflorian * Redistribution and use in source and binary forms, with or without 9*ae8c6e27Sflorian * modification, are permitted provided that the following conditions 10*ae8c6e27Sflorian * are met: 11*ae8c6e27Sflorian * 12*ae8c6e27Sflorian * Redistributions of source code must retain the above copyright notice, 13*ae8c6e27Sflorian * this list of conditions and the following disclaimer. 14*ae8c6e27Sflorian * 15*ae8c6e27Sflorian * Redistributions in binary form must reproduce the above copyright notice, 16*ae8c6e27Sflorian * this list of conditions and the following disclaimer in the documentation 17*ae8c6e27Sflorian * and/or other materials provided with the distribution. 18*ae8c6e27Sflorian * 19*ae8c6e27Sflorian * Neither the name of the NLNET LABS nor the names of its contributors may 20*ae8c6e27Sflorian * be used to endorse or promote products derived from this software without 21*ae8c6e27Sflorian * specific prior written permission. 22*ae8c6e27Sflorian * 23*ae8c6e27Sflorian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24*ae8c6e27Sflorian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25*ae8c6e27Sflorian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26*ae8c6e27Sflorian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27*ae8c6e27Sflorian * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28*ae8c6e27Sflorian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29*ae8c6e27Sflorian * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30*ae8c6e27Sflorian * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31*ae8c6e27Sflorian * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32*ae8c6e27Sflorian * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*ae8c6e27Sflorian * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34*ae8c6e27Sflorian */ 35*ae8c6e27Sflorian 36*ae8c6e27Sflorian /** 37*ae8c6e27Sflorian * \file 38*ae8c6e27Sflorian * 39*ae8c6e27Sflorian * This file contains prototypes for event loop functions. 40*ae8c6e27Sflorian * 41*ae8c6e27Sflorian */ 42*ae8c6e27Sflorian 43*ae8c6e27Sflorian #ifndef UB_EVENT_H 44*ae8c6e27Sflorian #define UB_EVENT_H 45*ae8c6e27Sflorian 46*ae8c6e27Sflorian struct ub_event_base; 47*ae8c6e27Sflorian struct ub_event; 48*ae8c6e27Sflorian struct comm_base; 49*ae8c6e27Sflorian struct event_base; 50*ae8c6e27Sflorian 51*ae8c6e27Sflorian /** event timeout */ 52*ae8c6e27Sflorian #define UB_EV_TIMEOUT 0x01 53*ae8c6e27Sflorian /** event fd readable */ 54*ae8c6e27Sflorian #define UB_EV_READ 0x02 55*ae8c6e27Sflorian /** event fd writable */ 56*ae8c6e27Sflorian #define UB_EV_WRITE 0x04 57*ae8c6e27Sflorian /** event signal */ 58*ae8c6e27Sflorian #define UB_EV_SIGNAL 0x08 59*ae8c6e27Sflorian /** event must persist */ 60*ae8c6e27Sflorian #define UB_EV_PERSIST 0x10 61*ae8c6e27Sflorian 62*ae8c6e27Sflorian /** Returns event-base type. Could be "mini-event", "winsock-event" for the 63*ae8c6e27Sflorian * daemon compile, and will be "pluggable-event<PACKAGE_VERSION>" for 64*ae8c6e27Sflorian * libunbound. 65*ae8c6e27Sflorian */ 66*ae8c6e27Sflorian const char* ub_event_get_version(void); 67*ae8c6e27Sflorian /** Return the name, system and method for the pluggable event base */ 68*ae8c6e27Sflorian void ub_get_event_sys(struct ub_event_base*, const char** n, const char** s, 69*ae8c6e27Sflorian const char** m); 70*ae8c6e27Sflorian /** Return a default event base. In the daemon this will be the only event 71*ae8c6e27Sflorian * bases used. 72*ae8c6e27Sflorian */ 73*ae8c6e27Sflorian struct ub_event_base* ub_default_event_base(int, time_t*, struct timeval*); 74*ae8c6e27Sflorian /** Return an ub_event_base constructed for the given libevent event base */ 75*ae8c6e27Sflorian struct ub_event_base* ub_libevent_event_base(struct event_base*); 76*ae8c6e27Sflorian /** Return the libevent base underlying the given ub_event_base. Will return 77*ae8c6e27Sflorian * NULL when the ub_event_base does not have an underlying libevent event base 78*ae8c6e27Sflorian */ 79*ae8c6e27Sflorian struct event_base* ub_libevent_get_event_base(struct ub_event_base*); 80*ae8c6e27Sflorian /** Free event base. Free events yourself */ 81*ae8c6e27Sflorian void ub_event_base_free(struct ub_event_base*); 82*ae8c6e27Sflorian /** Run the event base */ 83*ae8c6e27Sflorian int ub_event_base_dispatch(struct ub_event_base*); 84*ae8c6e27Sflorian /** exit that loop */ 85*ae8c6e27Sflorian int ub_event_base_loopexit(struct ub_event_base*); 86*ae8c6e27Sflorian 87*ae8c6e27Sflorian /** Create a new ub_event for the event base */ 88*ae8c6e27Sflorian struct ub_event* ub_event_new(struct ub_event_base*, 89*ae8c6e27Sflorian int fd, short bits, void (*cb)(int, short, void*), void* arg); 90*ae8c6e27Sflorian /** Create a new ub_event signal for the event base */ 91*ae8c6e27Sflorian struct ub_event* ub_signal_new(struct ub_event_base*, int fd, 92*ae8c6e27Sflorian void (*cb)(int, short, void*), void* arg); 93*ae8c6e27Sflorian /** Create a new ub_event associated with the wsaevent for the event base */ 94*ae8c6e27Sflorian struct ub_event* ub_winsock_register_wsaevent(struct ub_event_base*, 95*ae8c6e27Sflorian void* wsaevent, void (*cb)(int, short, void*), void* arg); 96*ae8c6e27Sflorian 97*ae8c6e27Sflorian /** Add event bits for this event to fire on */ 98*ae8c6e27Sflorian void ub_event_add_bits(struct ub_event*, short bits); 99*ae8c6e27Sflorian /** Configure the event so it will not longer fire on given bits */ 100*ae8c6e27Sflorian void ub_event_del_bits(struct ub_event*, short bits); 101*ae8c6e27Sflorian /** Change or set the file descriptor on the event */ 102*ae8c6e27Sflorian void ub_event_set_fd(struct ub_event*, int fd); 103*ae8c6e27Sflorian /** free the event */ 104*ae8c6e27Sflorian void ub_event_free(struct ub_event*); 105*ae8c6e27Sflorian /** Activate the event. The given timeval is an timeout value. */ 106*ae8c6e27Sflorian int ub_event_add(struct ub_event*, struct timeval*); 107*ae8c6e27Sflorian /** Deactivate the event */ 108*ae8c6e27Sflorian int ub_event_del(struct ub_event*); 109*ae8c6e27Sflorian /** Reconfigure and activate a timeout event */ 110*ae8c6e27Sflorian int ub_timer_add(struct ub_event*, struct ub_event_base*, 111*ae8c6e27Sflorian void (*cb)(int, short, void*), void* arg, struct timeval*); 112*ae8c6e27Sflorian /** Deactivate the timeout event */ 113*ae8c6e27Sflorian int ub_timer_del(struct ub_event*); 114*ae8c6e27Sflorian /** Activate a signal event */ 115*ae8c6e27Sflorian int ub_signal_add(struct ub_event*, struct timeval*); 116*ae8c6e27Sflorian /** Deactivate a signal event */ 117*ae8c6e27Sflorian int ub_signal_del(struct ub_event*); 118*ae8c6e27Sflorian /** Free a with a wsaevent associated event */ 119*ae8c6e27Sflorian void ub_winsock_unregister_wsaevent(struct ub_event* ev); 120*ae8c6e27Sflorian /** Signal the eventloop when a TCP windows socket will block on next read 121*ae8c6e27Sflorian * or write (given by the eventbits) 122*ae8c6e27Sflorian */ 123*ae8c6e27Sflorian void ub_winsock_tcp_wouldblock(struct ub_event*, int bits); 124*ae8c6e27Sflorian /** Equip the comm_base with the current time */ 125*ae8c6e27Sflorian void ub_comm_base_now(struct comm_base* cb); 126*ae8c6e27Sflorian 127*ae8c6e27Sflorian #endif /* UB_EVENT_H */ 128