Blame


1 c423c56e 2022-09-25 op /*
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.
4 c423c56e 2022-09-25 op *
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
7 c423c56e 2022-09-25 op * are met:
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.
15 c423c56e 2022-09-25 op *
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.
26 c423c56e 2022-09-25 op */
27 c423c56e 2022-09-25 op
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>
31 c423c56e 2022-09-25 op
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>
36 c423c56e 2022-09-25 op
37 c423c56e 2022-09-25 op /*
38 c423c56e 2022-09-25 op * Libevent glue for ASR.
39 c423c56e 2022-09-25 op */
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 *);
44 c423c56e 2022-09-25 op void *arg;
45 c423c56e 2022-09-25 op };
46 c423c56e 2022-09-25 op
47 c423c56e 2022-09-25 op static 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)
50 c423c56e 2022-09-25 op {
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;
54 c423c56e 2022-09-25 op
55 c423c56e 2022-09-25 op event_del(&eva->ev);
56 c423c56e 2022-09-25 op
57 c423c56e 2022-09-25 op if (asr_run(eva->async, &ar)) {
58 c423c56e 2022-09-25 op eva->cb(&ar, eva->arg);
59 c423c56e 2022-09-25 op free(eva);
60 c423c56e 2022-09-25 op } else {
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);
67 c423c56e 2022-09-25 op }
68 c423c56e 2022-09-25 op }
69 c423c56e 2022-09-25 op
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 *),
72 c423c56e 2022-09-25 op void *arg)
73 c423c56e 2022-09-25 op {
74 c423c56e 2022-09-25 op struct event_asr *eva;
75 c423c56e 2022-09-25 op struct timeval tv;
76 c423c56e 2022-09-25 op
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;
81 c423c56e 2022-09-25 op eva->cb = cb;
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);
87 c423c56e 2022-09-25 op return (eva);
88 c423c56e 2022-09-25 op }
89 c423c56e 2022-09-25 op
90 c423c56e 2022-09-25 op void
91 c423c56e 2022-09-25 op event_asr_abort(struct event_asr *eva)
92 c423c56e 2022-09-25 op {
93 c423c56e 2022-09-25 op asr_abort(eva->async);
94 c423c56e 2022-09-25 op event_del(&eva->ev);
95 c423c56e 2022-09-25 op free(eva);
96 c423c56e 2022-09-25 op }