Home | History | Annotate | Line # | Download | only in util
ub_event_pluggable.c revision 1.1.1.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  christos #define AS_MY_EVENT_BASE(x) \
    148  1.1  christos 	(((union {struct ub_event_base* a; struct my_event_base* b;})x).b)
    149  1.1  christos #define AS_MY_EVENT(x) \
    150  1.1  christos 	(((union {struct ub_event* a; struct my_event* b;})x).b)
    151  1.1  christos 
    152  1.1  christos const char* ub_event_get_version()
    153  1.1  christos {
    154  1.1  christos 	return "pluggable-event"PACKAGE_VERSION;
    155  1.1  christos }
    156  1.1  christos 
    157  1.1  christos static void
    158  1.1  christos my_event_add_bits(struct ub_event* ev, short bits)
    159  1.1  christos {
    160  1.1  christos 	AS_MY_EVENT(ev)->ev.ev_events |= NATIVE_BITS(bits);
    161  1.1  christos }
    162  1.1  christos 
    163  1.1  christos static void
    164  1.1  christos my_event_del_bits(struct ub_event* ev, short bits)
    165  1.1  christos {
    166  1.1  christos 	AS_MY_EVENT(ev)->ev.ev_events &= ~NATIVE_BITS(bits);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos static void
    170  1.1  christos my_event_set_fd(struct ub_event* ev, int fd)
    171  1.1  christos {
    172  1.1  christos 	AS_MY_EVENT(ev)->ev.ev_fd = fd;
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos static void
    176  1.1  christos my_event_free(struct ub_event* ev)
    177  1.1  christos {
    178  1.1  christos 	free(AS_MY_EVENT(ev));
    179  1.1  christos }
    180  1.1  christos 
    181  1.1  christos static int
    182  1.1  christos my_event_add(struct ub_event* ev, struct timeval* tv)
    183  1.1  christos {
    184  1.1  christos 	return event_add(&AS_MY_EVENT(ev)->ev, tv);
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos static int
    188  1.1  christos my_event_del(struct ub_event* ev)
    189  1.1  christos {
    190  1.1  christos 	return event_del(&AS_MY_EVENT(ev)->ev);
    191  1.1  christos }
    192  1.1  christos 
    193  1.1  christos static int
    194  1.1  christos my_timer_add(struct ub_event* ev, struct ub_event_base* base,
    195  1.1  christos 	void (*cb)(int, short, void*), void* arg, struct timeval* tv)
    196  1.1  christos {
    197  1.1  christos 	event_set(&AS_MY_EVENT(ev)->ev, -1, EV_TIMEOUT,NATIVE_BITS_CB(cb),arg);
    198  1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &AS_MY_EVENT(ev)->ev)
    199  1.1  christos 		!= 0)
    200  1.1  christos 		return -1;
    201  1.1  christos 	return evtimer_add(&AS_MY_EVENT(ev)->ev, tv);
    202  1.1  christos }
    203  1.1  christos 
    204  1.1  christos static int
    205  1.1  christos my_timer_del(struct ub_event* ev)
    206  1.1  christos {
    207  1.1  christos 	return evtimer_del(&AS_MY_EVENT(ev)->ev);
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos static int
    211  1.1  christos my_signal_add(struct ub_event* ev, struct timeval* tv)
    212  1.1  christos {
    213  1.1  christos 	return signal_add(&AS_MY_EVENT(ev)->ev, tv);
    214  1.1  christos }
    215  1.1  christos 
    216  1.1  christos static int
    217  1.1  christos my_signal_del(struct ub_event* ev)
    218  1.1  christos {
    219  1.1  christos 	return signal_del(&AS_MY_EVENT(ev)->ev);
    220  1.1  christos }
    221  1.1  christos 
    222  1.1  christos static void
    223  1.1  christos my_winsock_unregister_wsaevent(struct ub_event* ev)
    224  1.1  christos {
    225  1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    226  1.1  christos 	winsock_unregister_wsaevent(&AS_MY_EVENT(ev)->ev);
    227  1.1  christos 	free(AS_MY_EVENT(ev));
    228  1.1  christos #else
    229  1.1  christos 	(void)ev;
    230  1.1  christos #endif
    231  1.1  christos }
    232  1.1  christos 
    233  1.1  christos static void
    234  1.1  christos my_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
    235  1.1  christos {
    236  1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    237  1.1  christos 	winsock_tcp_wouldblock(&AS_MY_EVENT(ev)->ev, NATIVE_BITS(eventbits));
    238  1.1  christos #else
    239  1.1  christos 	(void)ev;
    240  1.1  christos 	(void)eventbits;
    241  1.1  christos #endif
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos static struct ub_event_vmt default_event_vmt = {
    245  1.1  christos 	my_event_add_bits, my_event_del_bits, my_event_set_fd,
    246  1.1  christos 	my_event_free, my_event_add, my_event_del,
    247  1.1  christos 	my_timer_add, my_timer_del, my_signal_add, my_signal_del,
    248  1.1  christos 	my_winsock_unregister_wsaevent, my_winsock_tcp_wouldblock
    249  1.1  christos };
    250  1.1  christos 
    251  1.1  christos static void
    252  1.1  christos my_event_base_free(struct ub_event_base* base)
    253  1.1  christos {
    254  1.1  christos #ifdef USE_MINI_EVENT
    255  1.1  christos 	event_base_free(AS_MY_EVENT_BASE(base)->base);
    256  1.1  christos #elif defined(HAVE_EVENT_BASE_FREE) && defined(HAVE_EVENT_BASE_ONCE)
    257  1.1  christos 	/* only libevent 1.2+ has it, but in 1.2 it is broken -
    258  1.1  christos 	   assertion fails on signal handling ev that is not deleted
    259  1.1  christos  	   in libevent 1.3c (event_base_once appears) this is fixed. */
    260  1.1  christos 	event_base_free(AS_MY_EVENT_BASE(base)->base);
    261  1.1  christos #endif /* HAVE_EVENT_BASE_FREE and HAVE_EVENT_BASE_ONCE */
    262  1.1  christos 	free(AS_MY_EVENT_BASE(base));
    263  1.1  christos }
    264  1.1  christos 
    265  1.1  christos static int
    266  1.1  christos my_event_base_dispatch(struct ub_event_base* base)
    267  1.1  christos {
    268  1.1  christos 	return event_base_dispatch(AS_MY_EVENT_BASE(base)->base);
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos static int
    272  1.1  christos my_event_base_loopexit(struct ub_event_base* base, struct timeval* tv)
    273  1.1  christos {
    274  1.1  christos 	return event_base_loopexit(AS_MY_EVENT_BASE(base)->base, tv);
    275  1.1  christos }
    276  1.1  christos 
    277  1.1  christos static struct ub_event*
    278  1.1  christos my_event_new(struct ub_event_base* base, int fd, short bits,
    279  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    280  1.1  christos {
    281  1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    282  1.1  christos 		sizeof(struct my_event));
    283  1.1  christos 
    284  1.1  christos 	if (!my_ev)
    285  1.1  christos 		return NULL;
    286  1.1  christos 
    287  1.1  christos 	event_set(&my_ev->ev, fd, NATIVE_BITS(bits), NATIVE_BITS_CB(cb), arg);
    288  1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
    289  1.1  christos 		free(my_ev);
    290  1.1  christos 		return NULL;
    291  1.1  christos 	}
    292  1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    293  1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    294  1.1  christos 	return &my_ev->super;
    295  1.1  christos }
    296  1.1  christos 
    297  1.1  christos static struct ub_event*
    298  1.1  christos my_signal_new(struct ub_event_base* base, int fd,
    299  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    300  1.1  christos {
    301  1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    302  1.1  christos 		sizeof(struct my_event));
    303  1.1  christos 
    304  1.1  christos 	if (!my_ev)
    305  1.1  christos 		return NULL;
    306  1.1  christos 
    307  1.1  christos 	signal_set(&my_ev->ev, fd, NATIVE_BITS_CB(cb), arg);
    308  1.1  christos 	if (event_base_set(AS_MY_EVENT_BASE(base)->base, &my_ev->ev) != 0) {
    309  1.1  christos 		free(my_ev);
    310  1.1  christos 		return NULL;
    311  1.1  christos 	}
    312  1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    313  1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    314  1.1  christos 	return &my_ev->super;
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos static struct ub_event*
    318  1.1  christos my_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
    319  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    320  1.1  christos {
    321  1.1  christos #if defined(USE_MINI_EVENT) && defined(USE_WINSOCK)
    322  1.1  christos 	struct my_event *my_ev = (struct my_event*)calloc(1,
    323  1.1  christos 		sizeof(struct my_event));
    324  1.1  christos 
    325  1.1  christos 	if (!my_ev)
    326  1.1  christos 		return NULL;
    327  1.1  christos 
    328  1.1  christos 	if (!winsock_register_wsaevent(AS_MY_EVENT_BASE(base)->base,
    329  1.1  christos 		&my_ev->ev, wsaevent, cb, arg)) {
    330  1.1  christos 		free(my_ev);
    331  1.1  christos 		return NULL;
    332  1.1  christos 
    333  1.1  christos 	}
    334  1.1  christos 	my_ev->super.magic = UB_EVENT_MAGIC;
    335  1.1  christos 	my_ev->super.vmt = &default_event_vmt;
    336  1.1  christos 	return &my_ev->super;
    337  1.1  christos #else
    338  1.1  christos 	(void)base;
    339  1.1  christos 	(void)wsaevent;
    340  1.1  christos 	(void)cb;
    341  1.1  christos 	(void)arg;
    342  1.1  christos 	return NULL;
    343  1.1  christos #endif
    344  1.1  christos }
    345  1.1  christos 
    346  1.1  christos static struct ub_event_base_vmt default_event_base_vmt = {
    347  1.1  christos 	my_event_base_free, my_event_base_dispatch,
    348  1.1  christos 	my_event_base_loopexit, my_event_new, my_signal_new,
    349  1.1  christos 	my_winsock_register_wsaevent
    350  1.1  christos };
    351  1.1  christos 
    352  1.1  christos struct ub_event_base*
    353  1.1  christos ub_default_event_base(int sigs, time_t* time_secs, struct timeval* time_tv)
    354  1.1  christos {
    355  1.1  christos 	struct my_event_base* my_base = (struct my_event_base*)calloc(1,
    356  1.1  christos 		sizeof(struct my_event_base));
    357  1.1  christos 
    358  1.1  christos 	if (!my_base)
    359  1.1  christos 		return NULL;
    360  1.1  christos 
    361  1.1  christos #ifdef USE_MINI_EVENT
    362  1.1  christos 	(void)sigs;
    363  1.1  christos 	/* use mini event time-sharing feature */
    364  1.1  christos 	my_base->base = event_init(time_secs, time_tv);
    365  1.1  christos #else
    366  1.1  christos 	(void)time_secs;
    367  1.1  christos 	(void)time_tv;
    368  1.1  christos #  if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
    369  1.1  christos 	/* libev */
    370  1.1  christos 	if(sigs)
    371  1.1  christos 		my_base->base = (struct event_base*)ev_default_loop(EVFLAG_AUTO);
    372  1.1  christos 	else
    373  1.1  christos 		my_base->base = (struct event_base*)ev_loop_new(EVFLAG_AUTO);
    374  1.1  christos #  else
    375  1.1  christos 	(void)sigs;
    376  1.1  christos #    ifdef HAVE_EVENT_BASE_NEW
    377  1.1  christos 	my_base->base = event_base_new();
    378  1.1  christos #    else
    379  1.1  christos 	my_base->base = event_init();
    380  1.1  christos #    endif
    381  1.1  christos #  endif
    382  1.1  christos #endif
    383  1.1  christos 	if (!my_base->base) {
    384  1.1  christos 		free(my_base);
    385  1.1  christos 		return NULL;
    386  1.1  christos 	}
    387  1.1  christos 	my_base->super.magic = UB_EVENT_MAGIC;
    388  1.1  christos 	my_base->super.vmt = &default_event_base_vmt;
    389  1.1  christos 	return &my_base->super;
    390  1.1  christos }
    391  1.1  christos 
    392  1.1  christos struct ub_event_base*
    393  1.1  christos ub_libevent_event_base(struct event_base* base)
    394  1.1  christos {
    395  1.1  christos #ifdef USE_MINI_EVENT
    396  1.1  christos 	(void)base;
    397  1.1  christos 	return NULL;
    398  1.1  christos #else
    399  1.1  christos 	struct my_event_base* my_base = (struct my_event_base*)calloc(1,
    400  1.1  christos 		sizeof(struct my_event_base));
    401  1.1  christos 
    402  1.1  christos 	if (!my_base)
    403  1.1  christos 		return NULL;
    404  1.1  christos 	my_base->super.magic = UB_EVENT_MAGIC;
    405  1.1  christos 	my_base->super.vmt = &default_event_base_vmt;
    406  1.1  christos 	my_base->base = base;
    407  1.1  christos 	return &my_base->super;
    408  1.1  christos #endif
    409  1.1  christos }
    410  1.1  christos 
    411  1.1  christos struct event_base*
    412  1.1  christos ub_libevent_get_event_base(struct ub_event_base* base)
    413  1.1  christos {
    414  1.1  christos #ifndef USE_MINI_EVENT
    415  1.1  christos 	if (base->vmt == &default_event_base_vmt)
    416  1.1  christos 		return AS_MY_EVENT_BASE(base)->base;
    417  1.1  christos #else
    418  1.1  christos 	(void)base;
    419  1.1  christos #endif
    420  1.1  christos 	return NULL;
    421  1.1  christos }
    422  1.1  christos 
    423  1.1  christos #if (defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)) && defined(EVBACKEND_SELECT)
    424  1.1  christos static const char* ub_ev_backend2str_pluggable(int b)
    425  1.1  christos {
    426  1.1  christos 	switch(b) {
    427  1.1  christos 	case EVBACKEND_SELECT:	return "select";
    428  1.1  christos 	case EVBACKEND_POLL:	return "poll";
    429  1.1  christos 	case EVBACKEND_EPOLL:	return "epoll";
    430  1.1  christos 	case EVBACKEND_KQUEUE:	return "kqueue";
    431  1.1  christos 	case EVBACKEND_DEVPOLL: return "devpoll";
    432  1.1  christos 	case EVBACKEND_PORT:	return "evport";
    433  1.1  christos 	}
    434  1.1  christos 	return "unknown";
    435  1.1  christos }
    436  1.1  christos #endif
    437  1.1  christos 
    438  1.1  christos void
    439  1.1  christos ub_get_event_sys(struct ub_event_base* ub_base, const char** n, const char** s,
    440  1.1  christos 	const char** m)
    441  1.1  christos {
    442  1.1  christos #ifdef USE_WINSOCK
    443  1.1  christos 	(void)ub_base;
    444  1.1  christos 	*n = "pluggable-event";
    445  1.1  christos 	*s = "winsock";
    446  1.1  christos 	*m = "WSAWaitForMultipleEvents";
    447  1.1  christos #elif defined(USE_MINI_EVENT)
    448  1.1  christos 	(void)ub_base;
    449  1.1  christos 	*n = "pluggable-event";
    450  1.1  christos 	*s = "internal";
    451  1.1  christos 	*m = "select";
    452  1.1  christos #else
    453  1.1  christos 	struct event_base* b = ub_libevent_get_event_base(ub_base);
    454  1.1  christos 	/* This function is only called from comm_base_create, so
    455  1.1  christos 	 * ub_base is guaranteed to exist and to be the default
    456  1.1  christos 	 * event base.
    457  1.1  christos 	 */
    458  1.1  christos 	assert(b);
    459  1.1  christos 	*n = "pluggable-event";
    460  1.1  christos 	*s = event_get_version();
    461  1.1  christos #  if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
    462  1.1  christos 	*n = "pluggable-libev";
    463  1.1  christos #    ifdef EVBACKEND_SELECT
    464  1.1  christos 	*m = ub_ev_backend2str_pluggable(ev_backend((struct ev_loop*)b));
    465  1.1  christos #    else
    466  1.1  christos 	*m = "not obtainable";
    467  1.1  christos #    endif
    468  1.1  christos #  elif defined(HAVE_EVENT_BASE_GET_METHOD)
    469  1.1  christos 	*n = "pluggable-libevent";
    470  1.1  christos 	*m = event_base_get_method(b);
    471  1.1  christos #  else
    472  1.1  christos 	*m = "not obtainable";
    473  1.1  christos #  endif
    474  1.1  christos #endif
    475  1.1  christos }
    476  1.1  christos 
    477  1.1  christos void
    478  1.1  christos ub_event_base_free(struct ub_event_base* base)
    479  1.1  christos {
    480  1.1  christos 	if (base && base->magic == UB_EVENT_MAGIC) {
    481  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    482  1.1  christos 			base->vmt->free == my_event_base_free);
    483  1.1  christos 		(*base->vmt->free)(base);
    484  1.1  christos 	}
    485  1.1  christos }
    486  1.1  christos 
    487  1.1  christos int
    488  1.1  christos ub_event_base_dispatch(struct ub_event_base* base)
    489  1.1  christos {
    490  1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    491  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    492  1.1  christos 			base->vmt->dispatch == my_event_base_dispatch);
    493  1.1  christos 		return (*base->vmt->dispatch)(base);
    494  1.1  christos 	}
    495  1.1  christos 	return -1;
    496  1.1  christos }
    497  1.1  christos 
    498  1.1  christos int
    499  1.1  christos ub_event_base_loopexit(struct ub_event_base* base)
    500  1.1  christos {
    501  1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    502  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    503  1.1  christos 			base->vmt->loopexit == my_event_base_loopexit);
    504  1.1  christos 		return (*base->vmt->loopexit)(base, NULL);
    505  1.1  christos 	}
    506  1.1  christos 	return -1;
    507  1.1  christos }
    508  1.1  christos 
    509  1.1  christos struct ub_event*
    510  1.1  christos ub_event_new(struct ub_event_base* base, int fd, short bits,
    511  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    512  1.1  christos {
    513  1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    514  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    515  1.1  christos 			base->vmt->new_event == my_event_new);
    516  1.1  christos 		return (*base->vmt->new_event)(base, fd, bits, cb, arg);
    517  1.1  christos 	}
    518  1.1  christos 	return NULL;
    519  1.1  christos }
    520  1.1  christos 
    521  1.1  christos struct ub_event*
    522  1.1  christos ub_signal_new(struct ub_event_base* base, int fd,
    523  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    524  1.1  christos {
    525  1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    526  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    527  1.1  christos 			base->vmt->new_signal == my_signal_new);
    528  1.1  christos 		return (*base->vmt->new_signal)(base, fd, cb, arg);
    529  1.1  christos 	}
    530  1.1  christos 	return NULL;
    531  1.1  christos }
    532  1.1  christos 
    533  1.1  christos struct ub_event*
    534  1.1  christos ub_winsock_register_wsaevent(struct ub_event_base* base, void* wsaevent,
    535  1.1  christos 	void (*cb)(int, short, void*), void* arg)
    536  1.1  christos {
    537  1.1  christos 	if (base->magic == UB_EVENT_MAGIC) {
    538  1.1  christos 		fptr_ok(base->vmt != &default_event_base_vmt ||
    539  1.1  christos 			base->vmt->winsock_register_wsaevent ==
    540  1.1  christos 			my_winsock_register_wsaevent);
    541  1.1  christos 		return (*base->vmt->winsock_register_wsaevent)(base, wsaevent, cb, arg);
    542  1.1  christos 	}
    543  1.1  christos 	return NULL;
    544  1.1  christos }
    545  1.1  christos 
    546  1.1  christos void
    547  1.1  christos ub_event_add_bits(struct ub_event* ev, short bits)
    548  1.1  christos {
    549  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    550  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    551  1.1  christos 			ev->vmt->add_bits == my_event_add_bits);
    552  1.1  christos 		(*ev->vmt->add_bits)(ev, bits);
    553  1.1  christos 	}
    554  1.1  christos }
    555  1.1  christos 
    556  1.1  christos void
    557  1.1  christos ub_event_del_bits(struct ub_event* ev, short bits)
    558  1.1  christos {
    559  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    560  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    561  1.1  christos 			ev->vmt->del_bits == my_event_del_bits);
    562  1.1  christos 		(*ev->vmt->del_bits)(ev, bits);
    563  1.1  christos 	}
    564  1.1  christos }
    565  1.1  christos 
    566  1.1  christos void
    567  1.1  christos ub_event_set_fd(struct ub_event* ev, int fd)
    568  1.1  christos {
    569  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    570  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    571  1.1  christos 			ev->vmt->set_fd == my_event_set_fd);
    572  1.1  christos 		(*ev->vmt->set_fd)(ev, fd);
    573  1.1  christos 	}
    574  1.1  christos }
    575  1.1  christos 
    576  1.1  christos void
    577  1.1  christos ub_event_free(struct ub_event* ev)
    578  1.1  christos {
    579  1.1  christos 	if (ev && ev->magic == UB_EVENT_MAGIC) {
    580  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    581  1.1  christos 			ev->vmt->free == my_event_free);
    582  1.1  christos 		(*ev->vmt->free)(ev);
    583  1.1  christos 	}
    584  1.1  christos }
    585  1.1  christos 
    586  1.1  christos int
    587  1.1  christos ub_event_add(struct ub_event* ev, struct timeval* tv)
    588  1.1  christos {
    589  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    590  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    591  1.1  christos 			ev->vmt->add == my_event_add);
    592  1.1  christos 		return (*ev->vmt->add)(ev, tv);
    593  1.1  christos 	}
    594  1.1  christos        return -1;
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos int
    598  1.1  christos ub_event_del(struct ub_event* ev)
    599  1.1  christos {
    600  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    601  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    602  1.1  christos 			ev->vmt->del == my_event_del);
    603  1.1  christos 		return (*ev->vmt->del)(ev);
    604  1.1  christos 	}
    605  1.1  christos 	return -1;
    606  1.1  christos }
    607  1.1  christos 
    608  1.1  christos int
    609  1.1  christos ub_timer_add(struct ub_event* ev, struct ub_event_base* base,
    610  1.1  christos 	void (*cb)(int, short, void*), void* arg, struct timeval* tv)
    611  1.1  christos {
    612  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    613  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    614  1.1  christos 			ev->vmt->add_timer == my_timer_add);
    615  1.1  christos 		return (*ev->vmt->add_timer)(ev, base, cb, arg, tv);
    616  1.1  christos 	}
    617  1.1  christos 	return -1;
    618  1.1  christos }
    619  1.1  christos 
    620  1.1  christos int
    621  1.1  christos ub_timer_del(struct ub_event* ev)
    622  1.1  christos {
    623  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    624  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    625  1.1  christos 			ev->vmt->del_timer == my_timer_del);
    626  1.1  christos 		return (*ev->vmt->del_timer)(ev);
    627  1.1  christos 	}
    628  1.1  christos 	return -1;
    629  1.1  christos }
    630  1.1  christos 
    631  1.1  christos int
    632  1.1  christos ub_signal_add(struct ub_event* ev, struct timeval* tv)
    633  1.1  christos {
    634  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    635  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    636  1.1  christos 			ev->vmt->add_signal == my_signal_add);
    637  1.1  christos 		return (*ev->vmt->add_signal)(ev, tv);
    638  1.1  christos 	}
    639  1.1  christos 	return -1;
    640  1.1  christos }
    641  1.1  christos 
    642  1.1  christos int
    643  1.1  christos ub_signal_del(struct ub_event* ev)
    644  1.1  christos {
    645  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    646  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    647  1.1  christos 			ev->vmt->del_signal == my_signal_del);
    648  1.1  christos 		return (*ev->vmt->del_signal)(ev);
    649  1.1  christos 	}
    650  1.1  christos 	return -1;
    651  1.1  christos }
    652  1.1  christos 
    653  1.1  christos void
    654  1.1  christos ub_winsock_unregister_wsaevent(struct ub_event* ev)
    655  1.1  christos {
    656  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    657  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    658  1.1  christos 			ev->vmt->winsock_unregister_wsaevent ==
    659  1.1  christos 			my_winsock_unregister_wsaevent);
    660  1.1  christos 		(*ev->vmt->winsock_unregister_wsaevent)(ev);
    661  1.1  christos 	}
    662  1.1  christos }
    663  1.1  christos 
    664  1.1  christos void
    665  1.1  christos ub_winsock_tcp_wouldblock(struct ub_event* ev, int eventbits)
    666  1.1  christos {
    667  1.1  christos 	if (ev->magic == UB_EVENT_MAGIC) {
    668  1.1  christos 		fptr_ok(ev->vmt != &default_event_vmt ||
    669  1.1  christos 			ev->vmt->winsock_tcp_wouldblock ==
    670  1.1  christos 			my_winsock_tcp_wouldblock);
    671  1.1  christos 		(*ev->vmt->winsock_tcp_wouldblock)(ev, eventbits);
    672  1.1  christos 	}
    673  1.1  christos }
    674  1.1  christos 
    675  1.1  christos void ub_comm_base_now(struct comm_base* cb)
    676  1.1  christos {
    677  1.1  christos 	time_t *tt;
    678  1.1  christos 	struct timeval *tv;
    679  1.1  christos 
    680  1.1  christos #ifdef USE_MINI_EVENT
    681  1.1  christos /** minievent updates the time when it blocks. */
    682  1.1  christos 	if (comm_base_internal(cb)->magic == UB_EVENT_MAGIC &&
    683  1.1  christos 	    comm_base_internal(cb)->vmt == &default_event_base_vmt)
    684  1.1  christos 		return; /* Actually using mini event, so do not set time */
    685  1.1  christos #endif /* USE_MINI_EVENT */
    686  1.1  christos 
    687  1.1  christos /** fillup the time values in the event base */
    688  1.1  christos 	comm_base_timept(cb, &tt, &tv);
    689  1.1  christos 	if(gettimeofday(tv, NULL) < 0) {
    690  1.1  christos 		log_err("gettimeofday: %s", strerror(errno));
    691  1.1  christos 	}
    692  1.1  christos 	*tt = tv->tv_sec;
    693  1.1  christos }
    694  1.1  christos 
    695