diff options
author | 2014-04-03 11:27:02 +0000 | |
---|---|---|
committer | 2014-04-03 11:27:02 +0000 | |
commit | 57d5065fee060916b675f06834c55db7672d4e40 (patch) | |
tree | 097487e05eeef2de1edae51ce785a2b1f45d57a7 | |
parent | More uvm_extern.h cleanup. (diff) | |
download | wireguard-openbsd-57d5065fee060916b675f06834c55db7672d4e40.tar.xz wireguard-openbsd-57d5065fee060916b675f06834c55db7672d4e40.zip |
add helper functions to use asr with libevent.
ok deraadt@
-rw-r--r-- | lib/libevent/Makefile | 6 | ||||
-rw-r--r-- | lib/libevent/event.3 | 33 | ||||
-rw-r--r-- | lib/libevent/event.c | 67 | ||||
-rw-r--r-- | lib/libevent/event.h | 23 | ||||
-rw-r--r-- | lib/libevent/shlib_version | 2 |
5 files changed, 123 insertions, 8 deletions
diff --git a/lib/libevent/Makefile b/lib/libevent/Makefile index 7e2ce6acd4b..f7b66cd5ea2 100644 --- a/lib/libevent/Makefile +++ b/lib/libevent/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.31 2012/08/02 13:38:38 okan Exp $ +# $OpenBSD: Makefile,v 1.32 2014/04/03 11:27:02 eric Exp $ LIB= event SRCS= buffer.c evbuffer.c event.c event_tagging.c evutil.c kqueue.c \ @@ -54,7 +54,9 @@ MLINKS= event.3 bufferevent_base_set.3 \ event.3 signal_del.3 \ event.3 signal_initialized.3 \ event.3 signal_pending.3 \ - event.3 signal_set.3 + event.3 signal_set.3 \ + event.3 event_asr_run.3 \ + event.3 event_asr_abort.3 CFLAGS+= -I${.CURDIR} -DNDEBUG \ -DHAVE_CLOCK_GETTIME \ diff --git a/lib/libevent/event.3 b/lib/libevent/event.3 index 30fcf62a935..577cc82ed04 100644 --- a/lib/libevent/event.3 +++ b/lib/libevent/event.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: event.3,v 1.41 2010/04/22 16:35:45 jmc Exp $ +.\" $OpenBSD: event.3,v 1.42 2014/04/03 11:27:02 eric Exp $ .\" .\" Copyright (c) 2000 Artur Grabowski <art@openbsd.org> .\" All rights reserved. @@ -23,7 +23,7 @@ .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: April 22 2010 $ +.Dd $Mdocdate: April 3 2014 $ .Dt EVENT 3 .Os .Sh NAME @@ -76,7 +76,9 @@ .Nm evbuffer_write , .Nm evbuffer_read , .Nm evbuffer_find , -.Nm evbuffer_readline +.Nm evbuffer_readline , +.Nm event_asr_run , +.Nm event_asr_abort .Nd execute a function when a specific event occurs .Sh SYNOPSIS .Fd #include <sys/time.h> @@ -181,6 +183,10 @@ .Fn "evbuffer_find" "struct evbuffer *buf" "const u_char *data" "size_t size" .Ft "char *" .Fn "evbuffer_readline" "struct evbuffer *buf" +.Ft "struct event_asr *" +.Fn event_asr_run "struct asr_query *aq" "void (*fn)(struct asr_result *, void *)" "void *" +.Ft "void" +.Fn event_asr_abort "struct event_asr *eva" .Sh DESCRIPTION The .Nm event @@ -398,6 +404,26 @@ will proceed normally. .Pp It is the responsibility of the caller to provide these functions with pre-allocated event structures. +.Pp +The +.Fn event_asr_run +function is used to schedule the asynchronous resolver query +.Ar aq +to run within a libevent event loop, and call the +.Ar fn +callback when the result is available. +The extra +.Ar arg +parameter is passed to the callback. +The user does not need to set up an event structure for using this function. +It returns an opaque handle representing the running query. +This handle becomes invalid before the callback is run. +It can be cancelled by calling the +.Fn event_asr_abort +function. +See +.Xr asr_run 3 +for details on constructing asynchronous resolver queries. .Sh EVENT PRIORITIES By default .Nm libevent @@ -535,6 +561,7 @@ return 0. Otherwise, \-1 is returned and the global variable errno is set to indicate the error. .Sh SEE ALSO +.Xr asr_run 3 , .Xr kqueue 2 , .Xr poll 2 , .Xr select 2 , diff --git a/lib/libevent/event.c b/lib/libevent/event.c index 9a6c70b316f..97ed8f3d061 100644 --- a/lib/libevent/event.c +++ b/lib/libevent/event.c @@ -1,4 +1,4 @@ -/* $OpenBSD: event.c,v 1.27 2013/04/17 15:31:49 deraadt Exp $ */ +/* $OpenBSD: event.c,v 1.28 2014/04/03 11:27:02 eric Exp $ */ /* * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu> @@ -36,6 +36,7 @@ #undef WIN32_LEAN_AND_MEAN #endif #include <sys/types.h> +#include <sys/socket.h> #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #else @@ -52,6 +53,8 @@ #include <string.h> #include <assert.h> #include <time.h> +#include <netdb.h> +#include <asr.h> #include "event.h" #include "event-internal.h" @@ -1033,3 +1036,65 @@ event_get_method(void) { return (current_base->evsel->name); } + + +/* + * Libevent glue for ASR. + */ +struct event_asr { + struct event ev; + struct asr_query *async; + void (*cb)(struct asr_result *, void *); + void *arg; +}; + +static void +event_asr_dispatch(int fd __attribute__((__unused__)), + short ev __attribute__((__unused__)), void *arg) +{ + struct event_asr *eva = arg; + struct asr_result ar; + struct timeval tv; + + event_del(&eva->ev); + + if (asr_run(eva->async, &ar)) { + eva->cb(&ar, eva->arg); + free(eva); + } else { + event_set(&eva->ev, ar.ar_fd, + ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE, + event_asr_dispatch, eva); + tv.tv_sec = ar.ar_timeout / 1000; + tv.tv_usec = (ar.ar_timeout % 1000) * 1000; + event_add(&eva->ev, &tv); + } +} + +struct event_asr * +event_asr_run(struct asr_query *async, void (*cb)(struct asr_result *, void *), + void *arg) +{ + struct event_asr *eva; + struct timeval tv; + + eva = calloc(1, sizeof *eva); + if (eva == NULL) + return (NULL); + eva->async = async; + eva->cb = cb; + eva->arg = arg; + tv.tv_sec = 0; + tv.tv_usec = 0; + evtimer_set(&eva->ev, event_asr_dispatch, eva); + evtimer_add(&eva->ev, &tv); + return (eva); +} + +void +event_asr_abort(struct event_asr *eva) +{ + asr_abort(eva->async); + event_del(&eva->ev); + free(eva); +} diff --git a/lib/libevent/event.h b/lib/libevent/event.h index adc2e26f0a3..4594bd7884d 100644 --- a/lib/libevent/event.h +++ b/lib/libevent/event.h @@ -1,4 +1,4 @@ -/* $OpenBSD: event.h,v 1.25 2012/08/28 09:09:56 pascal Exp $ */ +/* $OpenBSD: event.h,v 1.26 2014/04/03 11:27:02 eric Exp $ */ /* * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> @@ -703,6 +703,27 @@ int event_base_priority_init(struct event_base *, int); int event_priority_set(struct event *, int); +/* Simple helpers for ASR async resolution API. */ + +/* We don't want to pull asr.h here */ +struct asr_query; +struct asr_result; + +struct event_asr; + +/** + * Schedule an async query to run in the libevent event loop, and trigger + * a callback when done. Returns an opaque async event handle. + */ +struct event_asr * event_asr_run(struct asr_query *, + void (*)(struct asr_result *, void *), void *); + +/** + * Cancel a running async query associated to an handle. + */ +void event_asr_abort(struct event_asr *); + + /* These functions deal with buffering input and output */ struct evbuffer { diff --git a/lib/libevent/shlib_version b/lib/libevent/shlib_version index d9961ea9fef..890c57389b5 100644 --- a/lib/libevent/shlib_version +++ b/lib/libevent/shlib_version @@ -1,2 +1,2 @@ major=4 -minor=0 +minor=1 |