aboutsummaryrefslogtreecommitdiffstats
path: root/openbsd-compat/event_asr_run.c
blob: aef86154a9c2193032aa1baf0e1b3c51cdb402b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*	$OpenBSD$	*/
/*
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "includes.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <asr.h>
#include <event.h>
#include <stdlib.h>

struct event_asr {
	struct event	 ev;
	struct asr_query *async;
	void		(*cb)(struct asr_result *, void *);
	void		*arg;
};

struct event_asr * event_asr_run(struct asr_query *,
    void (*)(struct asr_result *, void *), void *);
void event_asr_abort(struct event_asr *);

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);
}