1 /* $OpenBSD: timer.c,v 1.12 2015/01/16 06:39:58 deraadt 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 34 void timer_callback(int, short, void *); 35 36 void 37 timer_set(struct iked *env, struct iked_timer *tmr, 38 void (*cb)(struct iked *, void *), void *arg) 39 { 40 tmr->tmr_env = env; 41 tmr->tmr_cb = cb; 42 tmr->tmr_cbarg = arg; 43 evtimer_set(&tmr->tmr_ev, timer_callback, tmr); 44 } 45 46 void 47 timer_add(struct iked *env, struct iked_timer *tmr, int timeout) 48 { 49 struct timeval tv = { timeout }; 50 51 if (evtimer_initialized(&tmr->tmr_ev) && 52 evtimer_pending(&tmr->tmr_ev, NULL)) 53 evtimer_del(&tmr->tmr_ev); 54 55 evtimer_add(&tmr->tmr_ev, &tv); 56 } 57 58 void 59 timer_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 66 void 67 timer_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