2 c423c56e 2022-09-25 op * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
3 c423c56e 2022-09-25 op * All rights reserved.
5 c423c56e 2022-09-25 op * Redistribution and use in source and binary forms, with or without
6 c423c56e 2022-09-25 op * modification, are permitted provided that the following conditions
8 c423c56e 2022-09-25 op * 1. Redistributions of source code must retain the above copyright
9 c423c56e 2022-09-25 op * notice, this list of conditions and the following disclaimer.
10 c423c56e 2022-09-25 op * 2. Redistributions in binary form must reproduce the above copyright
11 c423c56e 2022-09-25 op * notice, this list of conditions and the following disclaimer in the
12 c423c56e 2022-09-25 op * documentation and/or other materials provided with the distribution.
13 c423c56e 2022-09-25 op * 3. The name of the author may not be used to endorse or promote products
14 c423c56e 2022-09-25 op * derived from this software without specific prior written permission.
16 c423c56e 2022-09-25 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 c423c56e 2022-09-25 op * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 c423c56e 2022-09-25 op * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 c423c56e 2022-09-25 op * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 c423c56e 2022-09-25 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 c423c56e 2022-09-25 op * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 c423c56e 2022-09-25 op * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 c423c56e 2022-09-25 op * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 c423c56e 2022-09-25 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 c423c56e 2022-09-25 op * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 c423c56e 2022-09-25 op #include <sys/types.h>
29 c423c56e 2022-09-25 op #include <sys/time.h>
30 c423c56e 2022-09-25 op #include <sys/socket.h>
32 c423c56e 2022-09-25 op #include <netdb.h>
33 c423c56e 2022-09-25 op #include <asr.h>
34 c423c56e 2022-09-25 op #include <event.h>
35 c423c56e 2022-09-25 op #include <stdlib.h>
38 c423c56e 2022-09-25 op * Libevent glue for ASR.
40 c423c56e 2022-09-25 op struct event_asr {
41 c423c56e 2022-09-25 op struct event ev;
42 c423c56e 2022-09-25 op struct asr_query *async;
43 c423c56e 2022-09-25 op void (*cb)(struct asr_result *, void *);
48 c423c56e 2022-09-25 op event_asr_dispatch(int fd __attribute__((__unused__)),
49 c423c56e 2022-09-25 op short ev __attribute__((__unused__)), void *arg)
51 c423c56e 2022-09-25 op struct event_asr *eva = arg;
52 c423c56e 2022-09-25 op struct asr_result ar;
53 c423c56e 2022-09-25 op struct timeval tv;
55 c423c56e 2022-09-25 op event_del(&eva->ev);
57 c423c56e 2022-09-25 op if (asr_run(eva->async, &ar)) {
58 c423c56e 2022-09-25 op eva->cb(&ar, eva->arg);
61 c423c56e 2022-09-25 op event_set(&eva->ev, ar.ar_fd,
62 c423c56e 2022-09-25 op ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE,
63 c423c56e 2022-09-25 op event_asr_dispatch, eva);
64 c423c56e 2022-09-25 op tv.tv_sec = ar.ar_timeout / 1000;
65 c423c56e 2022-09-25 op tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
66 c423c56e 2022-09-25 op event_add(&eva->ev, &tv);
70 c423c56e 2022-09-25 op struct event_asr *
71 c423c56e 2022-09-25 op event_asr_run(struct asr_query *async, void (*cb)(struct asr_result *, void *),
74 c423c56e 2022-09-25 op struct event_asr *eva;
75 c423c56e 2022-09-25 op struct timeval tv;
77 c423c56e 2022-09-25 op eva = calloc(1, sizeof *eva);
78 c423c56e 2022-09-25 op if (eva == NULL)
79 c423c56e 2022-09-25 op return (NULL);
80 c423c56e 2022-09-25 op eva->async = async;
82 c423c56e 2022-09-25 op eva->arg = arg;
83 c423c56e 2022-09-25 op tv.tv_sec = 0;
84 c423c56e 2022-09-25 op tv.tv_usec = 0;
85 c423c56e 2022-09-25 op evtimer_set(&eva->ev, event_asr_dispatch, eva);
86 c423c56e 2022-09-25 op evtimer_add(&eva->ev, &tv);
91 c423c56e 2022-09-25 op event_asr_abort(struct event_asr *eva)
93 c423c56e 2022-09-25 op asr_abort(eva->async);
94 c423c56e 2022-09-25 op event_del(&eva->ev);