13b6c3722Schristos /* 23b6c3722Schristos * util/ub_event.h - indirection layer for pluggable events 33b6c3722Schristos * 43b6c3722Schristos * Copyright (c) 2007, NLnet Labs. All rights reserved. 53b6c3722Schristos * 63b6c3722Schristos * This software is open source. 73b6c3722Schristos * 83b6c3722Schristos * Redistribution and use in source and binary forms, with or without 93b6c3722Schristos * modification, are permitted provided that the following conditions 103b6c3722Schristos * are met: 113b6c3722Schristos * 123b6c3722Schristos * Redistributions of source code must retain the above copyright notice, 133b6c3722Schristos * this list of conditions and the following disclaimer. 143b6c3722Schristos * 153b6c3722Schristos * Redistributions in binary form must reproduce the above copyright notice, 163b6c3722Schristos * this list of conditions and the following disclaimer in the documentation 173b6c3722Schristos * and/or other materials provided with the distribution. 183b6c3722Schristos * 193b6c3722Schristos * Neither the name of the NLNET LABS nor the names of its contributors may 203b6c3722Schristos * be used to endorse or promote products derived from this software without 213b6c3722Schristos * specific prior written permission. 223b6c3722Schristos * 233b6c3722Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 243b6c3722Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 253b6c3722Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 263b6c3722Schristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 273b6c3722Schristos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 283b6c3722Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 293b6c3722Schristos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 303b6c3722Schristos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 313b6c3722Schristos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 323b6c3722Schristos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 333b6c3722Schristos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 343b6c3722Schristos */ 353b6c3722Schristos 363b6c3722Schristos /** 373b6c3722Schristos * \file 383b6c3722Schristos * 393b6c3722Schristos * This file contains prototypes for event loop functions. 403b6c3722Schristos * 413b6c3722Schristos */ 423b6c3722Schristos 433b6c3722Schristos #ifndef UB_EVENT_H 443b6c3722Schristos #define UB_EVENT_H 453b6c3722Schristos 463b6c3722Schristos struct ub_event_base; 473b6c3722Schristos struct ub_event; 483b6c3722Schristos struct comm_base; 493b6c3722Schristos struct event_base; 503b6c3722Schristos 513b6c3722Schristos /** event timeout */ 523b6c3722Schristos #define UB_EV_TIMEOUT 0x01 533b6c3722Schristos /** event fd readable */ 543b6c3722Schristos #define UB_EV_READ 0x02 553b6c3722Schristos /** event fd writable */ 563b6c3722Schristos #define UB_EV_WRITE 0x04 573b6c3722Schristos /** event signal */ 583b6c3722Schristos #define UB_EV_SIGNAL 0x08 593b6c3722Schristos /** event must persist */ 603b6c3722Schristos #define UB_EV_PERSIST 0x10 613b6c3722Schristos 623b6c3722Schristos /** Returns event-base type. Could be "mini-event", "winsock-event" for the 633b6c3722Schristos * daemon compile, and will be "pluggable-event<PACKAGE_VERSION>" for 643b6c3722Schristos * libunbound. 653b6c3722Schristos */ 66*0cd9f4ecSchristos const char* ub_event_get_version(void); 673b6c3722Schristos /** Return the name, system and method for the pluggable event base */ 683b6c3722Schristos void ub_get_event_sys(struct ub_event_base*, const char** n, const char** s, 693b6c3722Schristos const char** m); 70*0cd9f4ecSchristos /** Return a default event base. In the daemon this will be the only event 713b6c3722Schristos * bases used. 723b6c3722Schristos */ 733b6c3722Schristos struct ub_event_base* ub_default_event_base(int, time_t*, struct timeval*); 743b6c3722Schristos /** Return an ub_event_base constructed for the given libevent event base */ 753b6c3722Schristos struct ub_event_base* ub_libevent_event_base(struct event_base*); 763b6c3722Schristos /** Return the libevent base underlying the given ub_event_base. Will return 773b6c3722Schristos * NULL when the ub_event_base does not have an underlying libevent event base 783b6c3722Schristos */ 793b6c3722Schristos struct event_base* ub_libevent_get_event_base(struct ub_event_base*); 803b6c3722Schristos /** Free event base. Free events yourself */ 813b6c3722Schristos void ub_event_base_free(struct ub_event_base*); 823b6c3722Schristos /** Run the event base */ 833b6c3722Schristos int ub_event_base_dispatch(struct ub_event_base*); 843b6c3722Schristos /** exit that loop */ 853b6c3722Schristos int ub_event_base_loopexit(struct ub_event_base*); 863b6c3722Schristos 873b6c3722Schristos /** Create a new ub_event for the event base */ 883b6c3722Schristos struct ub_event* ub_event_new(struct ub_event_base*, 893b6c3722Schristos int fd, short bits, void (*cb)(int, short, void*), void* arg); 903b6c3722Schristos /** Create a new ub_event signal for the event base */ 913b6c3722Schristos struct ub_event* ub_signal_new(struct ub_event_base*, int fd, 923b6c3722Schristos void (*cb)(int, short, void*), void* arg); 933b6c3722Schristos /** Create a new ub_event associated with the wsaevent for the event base */ 943b6c3722Schristos struct ub_event* ub_winsock_register_wsaevent(struct ub_event_base*, 953b6c3722Schristos void* wsaevent, void (*cb)(int, short, void*), void* arg); 963b6c3722Schristos 973b6c3722Schristos /** Add event bits for this event to fire on */ 983b6c3722Schristos void ub_event_add_bits(struct ub_event*, short bits); 993b6c3722Schristos /** Configure the event so it will not longer fire on given bits */ 1003b6c3722Schristos void ub_event_del_bits(struct ub_event*, short bits); 1013b6c3722Schristos /** Change or set the file descriptor on the event */ 1023b6c3722Schristos void ub_event_set_fd(struct ub_event*, int fd); 1033b6c3722Schristos /** free the event */ 1043b6c3722Schristos void ub_event_free(struct ub_event*); 1053b6c3722Schristos /** Activate the event. The given timeval is an timeout value. */ 1063b6c3722Schristos int ub_event_add(struct ub_event*, struct timeval*); 1073b6c3722Schristos /** Deactivate the event */ 1083b6c3722Schristos int ub_event_del(struct ub_event*); 1093b6c3722Schristos /** Reconfigure and activate a timeout event */ 1103b6c3722Schristos int ub_timer_add(struct ub_event*, struct ub_event_base*, 1113b6c3722Schristos void (*cb)(int, short, void*), void* arg, struct timeval*); 1123b6c3722Schristos /** Deactivate the timeout event */ 1133b6c3722Schristos int ub_timer_del(struct ub_event*); 1143b6c3722Schristos /** Activate a signal event */ 1153b6c3722Schristos int ub_signal_add(struct ub_event*, struct timeval*); 1163b6c3722Schristos /** Deactivate a signal event */ 1173b6c3722Schristos int ub_signal_del(struct ub_event*); 1183b6c3722Schristos /** Free a with a wsaevent associated event */ 1193b6c3722Schristos void ub_winsock_unregister_wsaevent(struct ub_event* ev); 1203b6c3722Schristos /** Signal the eventloop when a TCP windows socket will block on next read 1213b6c3722Schristos * or write (given by the eventbits) 1223b6c3722Schristos */ 1233b6c3722Schristos void ub_winsock_tcp_wouldblock(struct ub_event*, int bits); 1243b6c3722Schristos /** Equip the comm_base with the current time */ 1253b6c3722Schristos void ub_comm_base_now(struct comm_base* cb); 1263b6c3722Schristos 1273b6c3722Schristos #endif /* UB_EVENT_H */ 128