1 24fb1a39 2025-06-02 kirill /* $OpenBSD$ */
3 24fb1a39 2025-06-02 kirill * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
5 24fb1a39 2025-06-02 kirill * Permission to use, copy, modify, and distribute this software for any
6 24fb1a39 2025-06-02 kirill * purpose with or without fee is hereby granted, provided that the above
7 24fb1a39 2025-06-02 kirill * copyright notice and this permission notice appear in all copies.
9 24fb1a39 2025-06-02 kirill * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 24fb1a39 2025-06-02 kirill * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 24fb1a39 2025-06-02 kirill * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 24fb1a39 2025-06-02 kirill * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 24fb1a39 2025-06-02 kirill * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 24fb1a39 2025-06-02 kirill * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 24fb1a39 2025-06-02 kirill * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 24fb1a39 2025-06-02 kirill #include "openbsd-compat.h"
20 24fb1a39 2025-06-02 kirill #include <sys/types.h>
21 24fb1a39 2025-06-02 kirill #include <sys/socket.h>
22 24fb1a39 2025-06-02 kirill #include <netdb.h>
24 24fb1a39 2025-06-02 kirill #include <asr.h>
25 24fb1a39 2025-06-02 kirill #include <event.h>
26 24fb1a39 2025-06-02 kirill #include <stdlib.h>
28 24fb1a39 2025-06-02 kirill struct event_asr {
29 24fb1a39 2025-06-02 kirill struct event ev;
30 24fb1a39 2025-06-02 kirill struct asr_query *async;
31 24fb1a39 2025-06-02 kirill void (*cb)(struct asr_result *, void *);
32 24fb1a39 2025-06-02 kirill void *arg;
35 24fb1a39 2025-06-02 kirill struct event_asr * event_asr_run(struct asr_query *,
36 24fb1a39 2025-06-02 kirill void (*)(struct asr_result *, void *), void *);
37 24fb1a39 2025-06-02 kirill void event_asr_abort(struct event_asr *);
39 24fb1a39 2025-06-02 kirill static void
40 24fb1a39 2025-06-02 kirill event_asr_dispatch(int fd __attribute__((__unused__)),
41 24fb1a39 2025-06-02 kirill short ev __attribute__((__unused__)), void *arg)
43 24fb1a39 2025-06-02 kirill struct event_asr *eva = arg;
44 24fb1a39 2025-06-02 kirill struct asr_result ar;
45 24fb1a39 2025-06-02 kirill struct timeval tv;
47 24fb1a39 2025-06-02 kirill event_del(&eva->ev);
49 24fb1a39 2025-06-02 kirill if (asr_run(eva->async, &ar)) {
50 24fb1a39 2025-06-02 kirill eva->cb(&ar, eva->arg);
51 24fb1a39 2025-06-02 kirill free(eva);
53 24fb1a39 2025-06-02 kirill event_set(&eva->ev, ar.ar_fd,
54 24fb1a39 2025-06-02 kirill ar.ar_cond == ASR_WANT_READ ? EV_READ : EV_WRITE,
55 24fb1a39 2025-06-02 kirill event_asr_dispatch, eva);
56 24fb1a39 2025-06-02 kirill tv.tv_sec = ar.ar_timeout / 1000;
57 24fb1a39 2025-06-02 kirill tv.tv_usec = (ar.ar_timeout % 1000) * 1000;
58 24fb1a39 2025-06-02 kirill event_add(&eva->ev, &tv);
62 24fb1a39 2025-06-02 kirill struct event_asr *
63 24fb1a39 2025-06-02 kirill event_asr_run(struct asr_query *async, void (*cb)(struct asr_result *, void *),
64 24fb1a39 2025-06-02 kirill void *arg)
66 24fb1a39 2025-06-02 kirill struct event_asr *eva;
67 24fb1a39 2025-06-02 kirill struct timeval tv;
69 24fb1a39 2025-06-02 kirill eva = calloc(1, sizeof *eva);
70 24fb1a39 2025-06-02 kirill if (eva == NULL)
71 24fb1a39 2025-06-02 kirill return (NULL);
72 24fb1a39 2025-06-02 kirill eva->async = async;
73 24fb1a39 2025-06-02 kirill eva->cb = cb;
74 24fb1a39 2025-06-02 kirill eva->arg = arg;
75 24fb1a39 2025-06-02 kirill tv.tv_sec = 0;
76 24fb1a39 2025-06-02 kirill tv.tv_usec = 0;
77 24fb1a39 2025-06-02 kirill evtimer_set(&eva->ev, event_asr_dispatch, eva);
78 24fb1a39 2025-06-02 kirill evtimer_add(&eva->ev, &tv);
79 24fb1a39 2025-06-02 kirill return (eva);
83 24fb1a39 2025-06-02 kirill event_asr_abort(struct event_asr *eva)
85 24fb1a39 2025-06-02 kirill asr_abort(eva->async);
86 24fb1a39 2025-06-02 kirill event_del(&eva->ev);
87 24fb1a39 2025-06-02 kirill free(eva);