Home | History | Annotate | Line # | Download | only in util
ub_event_pluggable.c revision 1.1.1.2.6.1
      1          1.1  christos /*
      2          1.1  christos  * util/ub_event_pluggable.c - call registered pluggable event functions
      3          1.1  christos  *
      4          1.1  christos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
      5          1.1  christos  *
      6          1.1  christos  * This software is open source.
      7          1.1  christos  *
      8          1.1  christos  * Redistribution and use in source and binary forms, with or without
      9          1.1  christos  * modification, are permitted provided that the following conditions
     10          1.1  christos  * are met:
     11          1.1  christos  *
     12          1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13          1.1  christos  * this list of conditions and the following disclaimer.
     14          1.1  christos  *
     15          1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16          1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17          1.1  christos  * and/or other materials provided with the distribution.
     18          1.1  christos  *
     19          1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20          1.1  christos  * be used to endorse or promote products derived from this software without
     21          1.1  christos  * specific prior written permission.
     22          1.1  christos  *
     23          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27          1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28          1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29          1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30          1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31          1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32          1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33          1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34          1.1  christos  */
     35          1.1  christos 
     36          1.1  christos /**
     37          1.1  christos  * \file
     38          1.1  christos  *
     39          1.1  christos  * This file contains an implementation for the indirection layer for pluggable
     40          1.1  christos  * events that calls the registered pluggable event loop.  It also defines a
     41          1.1  christos  * default pluggable event loop based on the default libevent (compatibility)
     42          1.1  christos  * functions.
     43          1.1  christos  */
     44          1.1  christos #include "config.h"
     45          1.1  christos #include <sys/time.h>
     46          1.1  christos #include "util/ub_event.h"
     47          1.1  christos #include "libunbound/unbound-event.h"
     48          1.1  christos #include "util/netevent.h"
     49          1.1  christos #include "util/log.h"
     50          1.1  christos #include "util/fptr_wlist.h"
     51          1.1  christos 
     52          1.1  christos /* We define libevent structures here to hide the libevent stuff. */
     53          1.1  christos 
     54          1.1  christos #ifdef USE_MINI_EVENT
     55          1.1  christos #  ifdef USE_WINSOCK
     56          1.1  christos #    include "util/winsock_event.h"
     57          1.1  christos #  else
     58          1.1  christos #    include "util/mini_event.h"
     59          1.1  christos #  endif /* USE_WINSOCK */
     60          1.1  christos #else /* USE_MINI_EVENT */
     61          1.1  christos    /* we use libevent */
     62          1.1  christos #  ifdef HAVE_EVENT_H
     63          1.1  christos #    include <event.h>
     64          1.1  christos #  else
     65          1.1  christos #    include "event2/event.h"
     66          1.1  christos #    include "event2/event_struct.h"
     67          1.1  christos #    include "event2/event_compat.h"
     68          1.1  christos #  endif
     69          1.1  christos #endif /* USE_MINI_EVENT */
     70          1.1  christos 
     71          1.1  christos #if UB_EV_TIMEOUT != EV_TIMEOUT || UB_EV_READ != EV_READ || \
     72          1.1  christos     UB_EV_WRITE != EV_WRITE || UB_EV_SIGNAL != EV_SIGNAL || \
     73          1.1  christos     UB_EV_PERSIST != EV_PERSIST
     74          1.1  christos /* Only necessary for libev */
     75          1.1  christos #  define NATIVE_BITS(b) ( \
     76          1.1  christos 	  (((b) & UB_EV_TIMEOUT) ? EV_TIMEOUT : 0) \
     77          1.1  christos 	| (((b) & UB_EV_READ   ) ? EV_READ    : 0) \
     78          1.1  christos 	| (((b) & UB_EV_WRITE  ) ? EV_WRITE   : 0) \
     79          1.1  christos 	| (((b) & UB_EV_SIGNAL ) ? EV_SIGNAL  : 0) \
     80          1.1  christos 	| (((b) & UB_EV_PERSIST) ? EV_PERSIST : 0))
     81          1.1  christos 
     82          1.1  christos #  define UB_EV_BITS(b) ( \
     83          1.1  christos 	  (((b) & EV_TIMEOUT) ? UB_EV_TIMEOUT : 0) \
     84          1.1  christos 	| (((b) & EV_READ   ) ? UB_EV_READ    : 0) \
     85          1.1  christos 	| (((b) & EV_WRITE  ) ? UB_EV_WRITE   : 0) \
     86          1.1  christos 	| (((b) & EV_SIGNAL ) ? UB_EV_SIGNAL  : 0) \
     87          1.1  christos 	| (((b) & EV_PERSIST) ? UB_EV_PERSIST : 0))
     88          1.1  christos 
     89          1.1  christos #  define UB_EV_BITS_CB(C) void my_ ## C (int fd, short bits, void *arg) \
     90          1.1  christos 	{ (C)(fd, UB_EV_BITS(bits), arg); }
     91          1.1  christos 
     92          1.1  christos UB_EV_BITS_CB(comm_point_udp_callback);
     93          1.1  christos UB_EV_BITS_CB(comm_point_udp_ancil_callback)
     94          1.1  christos UB_EV_BITS_CB(comm_point_tcp_accept_callback)
     95          1.1  christos UB_EV_BITS_CB(comm_point_tcp_handle_callback)
     96          1.1  christos UB_EV_BITS_CB(comm_timer_callback)
     97          1.1  christos UB_EV_BITS_CB(comm_signal_callback)
     98          1.1  christos UB_EV_BITS_CB(comm_point_local_handle_callback)
     99          1.1  christos UB_EV_BITS_CB(comm_point_raw_handle_callback)
    100          1.1  christos UB_EV_BITS_CB(tube_handle_signal)
    101          1.1  christos UB_EV_BITS_CB(comm_base_handle_slow_accept)
    102          1.1  christos 
    103          1.1  christos static void (*NATIVE_BITS_CB(void (*cb)(int, short, void*)))(int, short, void*)
    104          1.1  christos {
    105          1.1  christos 	if(cb == comm_point_udp_callback)
    106          1.1  christos 		return my_comm_point_udp_callback;
    107          1.1  christos 	else if(cb == comm_point_udp_ancil_callback)
    108          1.1  christos 		return my_comm_point_udp_ancil_callback;
    109          1.1  christos 	else if(cb == comm_point_tcp_accept_callback)
    110          1.1  christos 		return my_comm_point_tcp_accept_callback;
    111          1.1  christos 	else if(cb == comm_point_tcp_handle_callback)
    112          1.1  christos 		return my_comm_point_tcp_handle_callback;
    113          1.1  christos 	else if(cb == comm_timer_callback)
    114          1.1  christos 		return my_comm_timer_callback;
    115          1.1  christos 	else if(cb == comm_signal_callback)
    116          1.1  christos 		return my_comm_signal_callback;
    117          1.1  christos 	else if(cb == comm_point_local_handle_callback)
    118          1.1  christos 		return my_comm_point_local_handle_callback;
    119          1.1  christos 	else if(cb == comm_point_raw_handle_callback)
    120          1.1  christos 		return my_comm_point_raw_handle_callback;
    121          1.1  christos 	else if(cb == tube_handle_signal)
    122          1.1  christos 		return my_tube_handle_signal;
    123          1.1  christos 	else if(cb == comm_base_handle_slow_accept)
    124          1.1  christos 		return my_comm_base_handle_slow_accept;
    125          1.1  christos 	else
    126          1.1  christos 		return NULL;
    127          1.1  christos }
    128          1.1  christos #else
    129          1.1  christos #  define NATIVE_BITS(b) (b)
    130          1.1  christos #  define NATIVE_BITS_CB(c) (c)
    131          1.1  christos #endif
    132          1.1  christos 
    133          1.1  christos #ifndef EVFLAG_AUTO
    134          1.1  christos #define EVFLAG_AUTO 0
    135          1.1  christos #endif
    136          1.1  christos 
    137          1.1  christos struct my_event_base {
    138          1.1  christos 	struct ub_event_base super;
    139          1.1  christos 	struct event_base* base;
    140          1.1  christos };
    141          1.1  christos 
    142          1.1  christos struct my_event {
    143          1.1  christos 	struct ub_event super;
    144          1.1  christos 	struct event ev;
    145          1.1  christos };
    146          1.1  christos 
    147      1.1.1.2  christos #define AS_MY_EVENT_BASE(x) ((struct my_event_base*)x)
    148      1.1.1.2  christos #define AS_MY_EVENT(x) ((struct my_event*)x)
    149          1.1  christos 
    150      1.1.1.2  christos const char* ub_event_get_version(void)
    151          1.1  christos {
    152          1.1  christos 	return "pluggable-event"PACKAGE_VERSION;
    153          1.1  christos }
    154          1.1  christos 
    155          1.1  christos static void
    156          1.1  christos my_event_add_bits(struct ub_event* ev, short bits)
    157          1.1  christos {
    158          1.1  christos 	AS_MY_EVENT(ev)->ev.ev_events |= NATIVE_BITS(bits);
    159          1.1  christos }
    160          1.1  christos 
    161          1.1  christos static void
    162          1.1  christos my_event_del_bits(struct ub_event* ev, short bits)
    163          1.1  christos {
    164          1.1  christos 	AS_MY_EVENT(ev)->ev.ev_events &= ~NATIVE_BITS(bits);
    165          1.1  christos }
    166          1.1  christos 
    167          1.1  christos static void
    168          1.1  christos my_event_set_fd(struct ub_event* ev, int fd)
    169          1.1  christos {
    170          1.1  christos 	AS_MY_EVENT(ev)->ev.ev_fd = fd;
    171          1.1  christos }
    172          1.1  christos 
    173          1.1  christos static void
    174          1.1  christos my_event_free(struct ub_event* ev)
    175          1.1  christos {
    176          1.1  christos 	free(AS_MY_EVENT(ev));
    177          1.1  christos }
    178          1.1  christos 
    179          1.1  christos static int
    180          1.1  christos my_event_add(struct ub_event* ev, struct timeval* tv)
    181          1.1  christos {
    182          1.1  christos 	return event_add(&AS_MY_EVENT(ev)->ev, tv);
    183          1.1  christos }
    184          1.1  christos 
    185          1.1  christos static int
    186          1.1  christos my_event_del(struct ub_event* ev)
    187          1.1  christos {
    188          1.1  christos 	return event_del(&AS_MY_EVENT(ev)->ev);
    189          1.1  christos }
    190          1.1  christos 
    191          1.1  christos static int
    192          1.1  christos my_timer_add(struct ub_event* ev, struct ub_event_base* base,
    193          1.1  christos 	void (*cb)(int, short, void*), void* arg, struct timeval* tv)
    194          1.1  christos {
    195          1.1  christos 	event_set(&AS_MY_EVENT(ev)->ev, -1, EV_TIMEOUT,NATIVE_BITS_CB(cb),arg);
    196          1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &AS_MY_EVENT(ev)->ev)
    197          1.1  christos 		!= 0)
    198          1.1  christos 		return -1;
    199          1.1  christos 	return evtimer_add(&AS_MY_EVENT(ev)->ev, tv);
    200          1.1  christos }
    201          1.1  christos 
    202          1.1  christos static int
    203          1.1  christos my_timer_del(struct ub_event* ev)
    204          1.1  christos {
    205          1.1  christos 	return evtimer_del(&AS_MY_EVENT(ev)->ev);
    206          1.1  christos }
    207          1.1  christos 
    208          1.1  christos static int
    209          1.1  christos my_signal_add(struct ub_event* ev, struct timeval* tv)
    210          1.1  christos {
    211          1.1  christos 	return signal_add(&AS_MY_EVENT(ev)->ev, tv);
    212          1.1  christos }
    213          1.1  christos 
    214          1.1  christos static int
    215          1.1  christos my_signal_del(struct ub_event* ev)
    216          1.1  christos {
    217          1.1  christos 	return signal_del(&AS_MY_EVENT(ev)->ev);
    218          1.1  christos }
    219          1.1  christos 
    220          1.1  christos static void
    221          1.1  christos my_winsock_unregister_wsaevent(struct ub_event* ev)
    222          1.1  christos {
    223          1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    224          1.1  christos 	winsock_unregister_wsaevent(&AS_MY_EVENT(ev)->ev);
    225          1.1  christos 	free(AS_MY_EVENT(ev));
    226          1.1  christos #else
    227          1.1  christos 	(void)ev;
    228          1.1  christos #endif
    229          1.1  christos }
    230          1.1  christos 
    231          1.1  christos static void
    232          1.1  christos my_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
    233          1.1  christos {
    234          1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    235          1.1  christos 	winsock_tcp_wouldblock(&AS_MY_EVENT(ev)->ev, NATIVE_BITS(eventbits));
    236          1.1  christos #else
    237          1.1  christos 	(void)ev;
    238          1.1  christos 	(void)eventbits;
    239          1.1  christos #endif
    240          1.1  christos }
    241          1.1  christos 
    242          1.1  christos static struct ub_event_vmt default_event_vmt = {
    243          1.1  christos 	my_event_add_bits, my_event_del_bits, my_event_set_fd,
    244          1.1  christos 	my_event_free, my_event_add, my_event_del,
    245          1.1  christos 	my_timer_add, my_timer_del, my_signal_add, my_signal_del,
    246          1.1  christos 	my_winsock_unregister_wsaevent, my_winsock_tcp_wouldblock
    247          1.1  christos };
    248          1.1  christos 
    249          1.1  christos static void
    250          1.1  christos my_event_base_free(struct ub_event_base* base)
    251          1.1  christos {
    252          1.1  christos #ifdef USE_MINI_EVENT
    253          1.1  christos 	event_base_free(AS_MY_EVENT_BASE(base)->base);
    254          1.1  christos #elif defined(HAVE_EVENT_BASE_FREE) && defined(HAVE_EVENT_BASE_ONCE)
    255          1.1  christos 	/* only libevent 1.2+ has it, but in 1.2 it is broken -
    256          1.1  christos 	   assertion fails on signal handling ev that is not deleted
    257          1.1  christos  	   in libevent 1.3c (event_base_once appears) this is fixed. */
    258          1.1  christos 	event_base_free(AS_MY_EVENT_BASE(base)->base);
    259          1.1  christos #endif /* HAVE_EVENT_BASE_FREE and HAVE_EVENT_BASE_ONCE */
    260          1.1  christos 	free(AS_MY_EVENT_BASE(base));
    261          1.1  christos }
    262          1.1  christos 
    263          1.1  christos static int
    264          1.1  christos my_event_base_dispatch(struct ub_event_base* base)
    265          1.1  christos {
    266          1.1  christos 	return event_base_dispatch(AS_MY_EVENT_BASE(base)->base);
    267          1.1  christos }
    268          1.1  christos 
    269          1.1  christos static int
    270          1.1  christos my_event_base_loopexit(struct ub_event_base* base, struct timeval* tv)
    271          1.1  christos {
    272          1.1  christos 	return event_base_loopexit(AS_MY_EVENT_BASE(base)->base, tv);
    273          1.1  christos }
    274          1.1  christos 
    275          1.1  christos static struct ub_event*
    276          1.1  christos my_event_new(struct ub_event_base* base, int fd, short bits,
    277          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    278          1.1  christos {
    279          1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    280          1.1  christos 		sizeof(struct my_event));
    281          1.1  christos 
    282          1.1  christos 	if (!my_ev)
    283          1.1  christos 		return NULL;
    284          1.1  christos 
    285          1.1  christos 	event_set(&my_ev->ev, fd, NATIVE_BITS(bits), NATIVE_BITS_CB(cb), arg);
    286          1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
    287          1.1  christos 		free(my_ev);
    288          1.1  christos 		return NULL;
    289          1.1  christos 	}
    290          1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    291          1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    292          1.1  christos 	return &my_ev->super;
    293          1.1  christos }
    294          1.1  christos 
    295          1.1  christos static struct ub_event*
    296          1.1  christos my_signal_new(struct ub_event_base* base, int fd,
    297          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    298          1.1  christos {
    299          1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    300          1.1  christos 		sizeof(struct my_event));
    301          1.1  christos 
    302          1.1  christos 	if (!my_ev)
    303          1.1  christos 		return NULL;
    304          1.1  christos 
    305          1.1  christos 	signal_set(&my_ev->ev, fd, NATIVE_BITS_CB(cb), arg);
    306          1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
    307          1.1  christos 		free(my_ev);
    308          1.1  christos 		return NULL;
    309          1.1  christos 	}
    310          1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    311          1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    312          1.1  christos 	return &my_ev->super;
    313          1.1  christos }
    314          1.1  christos 
    315          1.1  christos static struct ub_event*
    316          1.1  christos my_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
    317          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    318          1.1  christos {
    319          1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    320          1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    321          1.1  christos 		sizeof(struct my_event));
    322          1.1  christos 
    323          1.1  christos 	if (!my_ev)
    324          1.1  christos 		return NULL;
    325          1.1  christos 
    326          1.1  christos 	if (!winsock_register_wsaevent(AS_MY_EVENT_BASE(base)->base,
    327          1.1  christos 		&my_ev->ev, wsaevent, cb, arg)) {
    328          1.1  christos 		free(my_ev);
    329          1.1  christos 		return NULL;
    330          1.1  christos 
    331          1.1  christos 	}
    332          1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    333          1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    334          1.1  christos 	return &my_ev->super;
    335          1.1  christos #else
    336          1.1  christos 	(void)base;
    337          1.1  christos 	(void)wsaevent;
    338          1.1  christos 	(void)cb;
    339          1.1  christos 	(void)arg;
    340          1.1  christos 	return NULL;
    341          1.1  christos #endif
    342          1.1  christos }
    343          1.1  christos 
    344          1.1  christos static struct ub_event_base_vmt default_event_base_vmt = {
    345          1.1  christos 	my_event_base_free, my_event_base_dispatch,
    346          1.1  christos 	my_event_base_loopexit, my_event_new, my_signal_new,
    347          1.1  christos 	my_winsock_register_wsaevent
    348          1.1  christos };
    349          1.1  christos 
    350          1.1  christos struct ub_event_base*
    351          1.1  christos ub_default_event_base(int sigs, time_t* time_secs, struct timeval* time_tv)
    352          1.1  christos {
    353          1.1  christos 	struct my_event_base* my_base = (struct my_event_base*)calloc(1,
    354          1.1  christos 		sizeof(struct my_event_base));
    355          1.1  christos 
    356          1.1  christos 	if (!my_base)
    357          1.1  christos 		return NULL;
    358          1.1  christos 
    359          1.1  christos #ifdef USE_MINI_EVENT
    360          1.1  christos 	(void)sigs;
    361          1.1  christos 	/* use mini event time-sharing feature */
    362          1.1  christos 	my_base->base = event_init(time_secs, time_tv);
    363          1.1  christos #else
    364          1.1  christos 	(void)time_secs;
    365          1.1  christos 	(void)time_tv;
    366          1.1  christos #  if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
    367          1.1  christos 	/* libev */
    368          1.1  christos 	if(sigs)
    369          1.1  christos 		my_base->base = (struct event_base*)ev_default_loop(EVFLAG_AUTO);
    370          1.1  christos 	else
    371          1.1  christos 		my_base->base = (struct event_base*)ev_loop_new(EVFLAG_AUTO);
    372          1.1  christos #  else
    373          1.1  christos 	(void)sigs;
    374          1.1  christos #    ifdef HAVE_EVENT_BASE_NEW
    375          1.1  christos 	my_base->base = event_base_new();
    376          1.1  christos #    else
    377          1.1  christos 	my_base->base = event_init();
    378          1.1  christos #    endif
    379          1.1  christos #  endif
    380          1.1  christos #endif
    381          1.1  christos 	if (!my_base->base) {
    382          1.1  christos 		free(my_base);
    383          1.1  christos 		return NULL;
    384          1.1  christos 	}
    385          1.1  christos 	my_base->super.magic = UB_EVENT_MAGIC;
    386          1.1  christos 	my_base->super.vmt = &default_event_base_vmt;
    387          1.1  christos 	return &my_base->super;
    388          1.1  christos }
    389          1.1  christos 
    390          1.1  christos struct ub_event_base*
    391          1.1  christos ub_libevent_event_base(struct event_base* base)
    392          1.1  christos {
    393          1.1  christos #ifdef USE_MINI_EVENT
    394          1.1  christos 	(void)base;
    395          1.1  christos 	return NULL;
    396          1.1  christos #else
    397          1.1  christos 	struct my_event_base* my_base = (struct my_event_base*)calloc(1,
    398          1.1  christos 		sizeof(struct my_event_base));
    399          1.1  christos 
    400          1.1  christos 	if (!my_base)
    401          1.1  christos 		return NULL;
    402          1.1  christos 	my_base->super.magic = UB_EVENT_MAGIC;
    403          1.1  christos 	my_base->super.vmt = &default_event_base_vmt;
    404          1.1  christos 	my_base->base = base;
    405          1.1  christos 	return &my_base->super;
    406          1.1  christos #endif
    407          1.1  christos }
    408          1.1  christos 
    409          1.1  christos struct event_base*
    410          1.1  christos ub_libevent_get_event_base(struct ub_event_base* base)
    411          1.1  christos {
    412          1.1  christos #ifndef USE_MINI_EVENT
    413          1.1  christos 	if (base->vmt == &default_event_base_vmt)
    414          1.1  christos 		return AS_MY_EVENT_BASE(base)->base;
    415          1.1  christos #else
    416          1.1  christos 	(void)base;
    417          1.1  christos #endif
    418          1.1  christos 	return NULL;
    419          1.1  christos }
    420          1.1  christos 
    421          1.1  christos #if (defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)) && defined(EVBACKEND_SELECT)
    422          1.1  christos static const char* ub_ev_backend2str_pluggable(int b)
    423          1.1  christos {
    424          1.1  christos 	switch(b) {
    425          1.1  christos 	case EVBACKEND_SELECT:	return "select";
    426          1.1  christos 	case EVBACKEND_POLL:	return "poll";
    427          1.1  christos 	case EVBACKEND_EPOLL:	return "epoll";
    428          1.1  christos 	case EVBACKEND_KQUEUE:	return "kqueue";
    429          1.1  christos 	case EVBACKEND_DEVPOLL: return "devpoll";
    430          1.1  christos 	case EVBACKEND_PORT:	return "evport";
    431          1.1  christos 	}
    432          1.1  christos 	return "unknown";
    433          1.1  christos }
    434          1.1  christos #endif
    435          1.1  christos 
    436          1.1  christos void
    437          1.1  christos ub_get_event_sys(struct ub_event_base* ub_base, const char** n, const char** s,
    438          1.1  christos 	const char** m)
    439          1.1  christos {
    440          1.1  christos #ifdef USE_WINSOCK
    441          1.1  christos 	(void)ub_base;
    442          1.1  christos 	*n = "pluggable-event";
    443          1.1  christos 	*s = "winsock";
    444          1.1  christos 	*m = "WSAWaitForMultipleEvents";
    445          1.1  christos #elif defined(USE_MINI_EVENT)
    446          1.1  christos 	(void)ub_base;
    447          1.1  christos 	*n = "pluggable-event";
    448          1.1  christos 	*s = "internal";
    449          1.1  christos 	*m = "select";
    450          1.1  christos #else
    451          1.1  christos 	struct event_base* b = ub_libevent_get_event_base(ub_base);
    452          1.1  christos 	/* This function is only called from comm_base_create, so
    453          1.1  christos 	 * ub_base is guaranteed to exist and to be the default
    454          1.1  christos 	 * event base.
    455          1.1  christos 	 */
    456  1.1.1.2.6.1    martin 	assert(b != NULL);
    457          1.1  christos 	*n = "pluggable-event";
    458          1.1  christos 	*s = event_get_version();
    459          1.1  christos #  if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
    460          1.1  christos 	*n = "pluggable-libev";
    461          1.1  christos #    ifdef EVBACKEND_SELECT
    462          1.1  christos 	*m = ub_ev_backend2str_pluggable(ev_backend((struct ev_loop*)b));
    463          1.1  christos #    else
    464          1.1  christos 	*m = "not obtainable";
    465          1.1  christos #    endif
    466          1.1  christos #  elif defined(HAVE_EVENT_BASE_GET_METHOD)
    467          1.1  christos 	*n = "pluggable-libevent";
    468          1.1  christos 	*m = event_base_get_method(b);
    469          1.1  christos #  else
    470          1.1  christos 	*m = "not obtainable";
    471          1.1  christos #  endif
    472          1.1  christos #endif
    473          1.1  christos }
    474          1.1  christos 
    475          1.1  christos void
    476          1.1  christos ub_event_base_free(struct ub_event_base* base)
    477          1.1  christos {
    478          1.1  christos 	if (base && base->magic == UB_EVENT_MAGIC) {
    479          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    480          1.1  christos 			base->vmt->free == my_event_base_free);
    481          1.1  christos 		(*base->vmt->free)(base);
    482          1.1  christos 	}
    483          1.1  christos }
    484          1.1  christos 
    485          1.1  christos int
    486          1.1  christos ub_event_base_dispatch(struct ub_event_base* base)
    487          1.1  christos {
    488          1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    489          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    490          1.1  christos 			base->vmt->dispatch == my_event_base_dispatch);
    491          1.1  christos 		return (*base->vmt->dispatch)(base);
    492          1.1  christos 	}
    493          1.1  christos 	return -1;
    494          1.1  christos }
    495          1.1  christos 
    496          1.1  christos int
    497          1.1  christos ub_event_base_loopexit(struct ub_event_base* base)
    498          1.1  christos {
    499          1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    500          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    501          1.1  christos 			base->vmt->loopexit == my_event_base_loopexit);
    502          1.1  christos 		return (*base->vmt->loopexit)(base, NULL);
    503          1.1  christos 	}
    504          1.1  christos 	return -1;
    505          1.1  christos }
    506          1.1  christos 
    507          1.1  christos struct ub_event*
    508          1.1  christos ub_event_new(struct ub_event_base* base, int fd, short bits,
    509          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    510          1.1  christos {
    511          1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    512          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    513          1.1  christos 			base->vmt->new_event == my_event_new);
    514          1.1  christos 		return (*base->vmt->new_event)(base, fd, bits, cb, arg);
    515          1.1  christos 	}
    516          1.1  christos 	return NULL;
    517          1.1  christos }
    518          1.1  christos 
    519          1.1  christos struct ub_event*
    520          1.1  christos ub_signal_new(struct ub_event_base* base, int fd,
    521          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    522          1.1  christos {
    523          1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    524          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    525          1.1  christos 			base->vmt->new_signal == my_signal_new);
    526          1.1  christos 		return (*base->vmt->new_signal)(base, fd, cb, arg);
    527          1.1  christos 	}
    528          1.1  christos 	return NULL;
    529          1.1  christos }
    530          1.1  christos 
    531          1.1  christos struct ub_event*
    532          1.1  christos ub_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
    533          1.1  christos 	void (*cb)(int, short, void*), void* arg)
    534          1.1  christos {
    535          1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    536          1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    537          1.1  christos 			base->vmt->winsock_register_wsaevent ==
    538          1.1  christos 			my_winsock_register_wsaevent);
    539          1.1  christos 		return (*base->vmt->winsock_register_wsaevent)(base, wsaevent, cb, arg);
    540          1.1  christos 	}
    541          1.1  christos 	return NULL;
    542          1.1  christos }
    543          1.1  christos 
    544          1.1  christos void
    545          1.1  christos ub_event_add_bits(struct ub_event* ev, short bits)
    546          1.1  christos {
    547          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    548          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    549          1.1  christos 			ev->vmt->add_bits == my_event_add_bits);
    550          1.1  christos 		(*ev->vmt->add_bits)(ev, bits);
    551          1.1  christos 	}
    552          1.1  christos }
    553          1.1  christos 
    554          1.1  christos void
    555          1.1  christos ub_event_del_bits(struct ub_event* ev, short bits)
    556          1.1  christos {
    557          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    558          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    559          1.1  christos 			ev->vmt->del_bits == my_event_del_bits);
    560          1.1  christos 		(*ev->vmt->del_bits)(ev, bits);
    561          1.1  christos 	}
    562          1.1  christos }
    563          1.1  christos 
    564          1.1  christos void
    565          1.1  christos ub_event_set_fd(struct ub_event* ev, int fd)
    566          1.1  christos {
    567          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    568          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    569          1.1  christos 			ev->vmt->set_fd == my_event_set_fd);
    570          1.1  christos 		(*ev->vmt->set_fd)(ev, fd);
    571          1.1  christos 	}
    572          1.1  christos }
    573          1.1  christos 
    574          1.1  christos void
    575          1.1  christos ub_event_free(struct ub_event* ev)
    576          1.1  christos {
    577          1.1  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    578          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    579          1.1  christos 			ev->vmt->free == my_event_free);
    580          1.1  christos 		(*ev->vmt->free)(ev);
    581          1.1  christos 	}
    582          1.1  christos }
    583          1.1  christos 
    584          1.1  christos int
    585          1.1  christos ub_event_add(struct ub_event* ev, struct timeval* tv)
    586          1.1  christos {
    587          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    588          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    589          1.1  christos 			ev->vmt->add == my_event_add);
    590          1.1  christos 		return (*ev->vmt->add)(ev, tv);
    591          1.1  christos 	}
    592          1.1  christos        return -1;
    593          1.1  christos }
    594          1.1  christos 
    595          1.1  christos int
    596          1.1  christos ub_event_del(struct ub_event* ev)
    597          1.1  christos {
    598      1.1.1.2  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    599          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    600          1.1  christos 			ev->vmt->del == my_event_del);
    601          1.1  christos 		return (*ev->vmt->del)(ev);
    602          1.1  christos 	}
    603          1.1  christos 	return -1;
    604          1.1  christos }
    605          1.1  christos 
    606          1.1  christos int
    607          1.1  christos ub_timer_add(struct ub_event* ev, struct ub_event_base* base,
    608          1.1  christos 	void (*cb)(int, short, void*), void* arg, struct timeval* tv)
    609          1.1  christos {
    610          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    611          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    612          1.1  christos 			ev->vmt->add_timer == my_timer_add);
    613          1.1  christos 		return (*ev->vmt->add_timer)(ev, base, cb, arg, tv);
    614          1.1  christos 	}
    615          1.1  christos 	return -1;
    616          1.1  christos }
    617          1.1  christos 
    618          1.1  christos int
    619          1.1  christos ub_timer_del(struct ub_event* ev)
    620          1.1  christos {
    621      1.1.1.2  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    622          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    623          1.1  christos 			ev->vmt->del_timer == my_timer_del);
    624          1.1  christos 		return (*ev->vmt->del_timer)(ev);
    625          1.1  christos 	}
    626          1.1  christos 	return -1;
    627          1.1  christos }
    628          1.1  christos 
    629          1.1  christos int
    630          1.1  christos ub_signal_add(struct ub_event* ev, struct timeval* tv)
    631          1.1  christos {
    632          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    633          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    634          1.1  christos 			ev->vmt->add_signal == my_signal_add);
    635          1.1  christos 		return (*ev->vmt->add_signal)(ev, tv);
    636          1.1  christos 	}
    637          1.1  christos 	return -1;
    638          1.1  christos }
    639          1.1  christos 
    640          1.1  christos int
    641          1.1  christos ub_signal_del(struct ub_event* ev)
    642          1.1  christos {
    643      1.1.1.2  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    644          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    645          1.1  christos 			ev->vmt->del_signal == my_signal_del);
    646          1.1  christos 		return (*ev->vmt->del_signal)(ev);
    647          1.1  christos 	}
    648          1.1  christos 	return -1;
    649          1.1  christos }
    650          1.1  christos 
    651          1.1  christos void
    652          1.1  christos ub_winsock_unregister_wsaevent(struct ub_event* ev)
    653          1.1  christos {
    654      1.1.1.2  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    655          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    656          1.1  christos 			ev->vmt->winsock_unregister_wsaevent ==
    657          1.1  christos 			my_winsock_unregister_wsaevent);
    658          1.1  christos 		(*ev->vmt->winsock_unregister_wsaevent)(ev);
    659          1.1  christos 	}
    660          1.1  christos }
    661          1.1  christos 
    662          1.1  christos void
    663          1.1  christos ub_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
    664          1.1  christos {
    665          1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    666          1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    667          1.1  christos 			ev->vmt->winsock_tcp_wouldblock ==
    668          1.1  christos 			my_winsock_tcp_wouldblock);
    669          1.1  christos 		(*ev->vmt->winsock_tcp_wouldblock)(ev, eventbits);
    670          1.1  christos 	}
    671          1.1  christos }
    672          1.1  christos 
    673          1.1  christos void ub_comm_base_now(struct comm_base* cb)
    674          1.1  christos {
    675          1.1  christos 	time_t *tt;
    676          1.1  christos 	struct timeval *tv;
    677          1.1  christos 
    678          1.1  christos #ifdef USE_MINI_EVENT
    679          1.1  christos /** minievent updates the time when it blocks. */
    680          1.1  christos 	if (comm_base_internal(cb)->magic == UB_EVENT_MAGIC &&
    681          1.1  christos 	    comm_base_internal(cb)->vmt == &default_event_base_vmt)
    682          1.1  christos 		return; /* Actually using mini event, so do not set time */
    683          1.1  christos #endif /* USE_MINI_EVENT */
    684          1.1  christos 
    685          1.1  christos /** fillup the time values in the event base */
    686          1.1  christos 	comm_base_timept(cb, &tt, &tv);
    687          1.1  christos 	if(gettimeofday(tv, NULL) < 0) {
    688          1.1  christos 		log_err("gettimeofday: %s", strerror(errno));
    689          1.1  christos 	}
    690  1.1.1.2.6.1    martin #ifndef S_SPLINT_S
    691          1.1  christos 	*tt = tv->tv_sec;
    692  1.1.1.2.6.1    martin #endif
    693          1.1  christos }
    694          1.1  christos 
    695