Blame


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