xref: /openbsd-src/sbin/iked/timer.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: timer.c,v 1.11 2014/01/24 05:58: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/param.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <event.h>
32 
33 #include "iked.h"
34 
35 void	 timer_callback(int, short, void *);
36 
37 void
38 timer_set(struct iked *env, struct iked_timer *tmr,
39     void (*cb)(struct iked *, void *), void *arg)
40 {
41 	tmr->tmr_env = env;
42 	tmr->tmr_cb = cb;
43 	tmr->tmr_cbarg = arg;
44 	evtimer_set(&tmr->tmr_ev, timer_callback, tmr);
45 }
46 
47 void
48 timer_add(struct iked *env, struct iked_timer *tmr, int timeout)
49 {
50 	struct timeval		 tv = { timeout };
51 
52 	if (evtimer_initialized(&tmr->tmr_ev) &&
53 	    evtimer_pending(&tmr->tmr_ev, NULL))
54 		evtimer_del(&tmr->tmr_ev);
55 
56 	evtimer_add(&tmr->tmr_ev, &tv);
57 }
58 
59 void
60 timer_del(struct iked *env, struct iked_timer *tmr)
61 {
62 	if (tmr->tmr_env == env && tmr->tmr_cb &&
63 	    evtimer_initialized(&tmr->tmr_ev))
64 		evtimer_del(&tmr->tmr_ev);
65 }
66 
67 void
68 timer_callback(int fd, short event, void *arg)
69 {
70 	struct iked_timer	*tmr = arg;
71 
72 	if (tmr->tmr_cb)
73 		tmr->tmr_cb(tmr->tmr_env, tmr->tmr_cbarg);
74 }
75