1/*	$OpenBSD: timer.c,v 1.13 2016/09/13 10:49:52 mikeb Exp $	*/
2
3/*
4 * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/queue.h>
20#include <sys/socket.h>
21#include <sys/uio.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <string.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <ctype.h>
30#include <event.h>
31
32#include "iked.h"
33
34void	 timer_callback(int, short, void *);
35
36void
37timer_set(struct iked *env, struct iked_timer *tmr,
38    void (*cb)(struct iked *, void *), void *arg)
39{
40	if (evtimer_initialized(&tmr->tmr_ev) &&
41	    evtimer_pending(&tmr->tmr_ev, NULL))
42		evtimer_del(&tmr->tmr_ev);
43
44	tmr->tmr_env = env;
45	tmr->tmr_cb = cb;
46	tmr->tmr_cbarg = arg;
47	evtimer_set(&tmr->tmr_ev, timer_callback, tmr);
48}
49
50void
51timer_add(struct iked *env, struct iked_timer *tmr, int timeout)
52{
53	struct timeval		 tv = { timeout };
54
55	evtimer_add(&tmr->tmr_ev, &tv);
56}
57
58void
59timer_del(struct iked *env, struct iked_timer *tmr)
60{
61	if (tmr->tmr_env == env && tmr->tmr_cb &&
62	    evtimer_initialized(&tmr->tmr_ev))
63		evtimer_del(&tmr->tmr_ev);
64}
65
66void
67timer_callback(int fd, short event, void *arg)
68{
69	struct iked_timer	*tmr = arg;
70
71	if (tmr->tmr_cb)
72		tmr->tmr_cb(tmr->tmr_env, tmr->tmr_cbarg);
73}
74