diff options
author | Mike Belopuhov <mikeb@cvs.openbsd.org> | 2012-05-29 15:09:13 +0000 |
---|---|---|
committer | Mike Belopuhov <mikeb@cvs.openbsd.org> | 2012-05-29 15:09:13 +0000 |
commit | 698e0023fa1a7b4f5ee9538ebddb2382616a5d12 (patch) | |
tree | 3bd6e90dcf38aaa7aa831d1b82393603a590b47f /sbin/iked/timer.c | |
parent | 3bec1f9ae8a78638c50da292ba007f063c667b93 (diff) |
improve timer framework; will be needed soon
Diffstat (limited to 'sbin/iked/timer.c')
-rw-r--r-- | sbin/iked/timer.c | 86 |
1 files changed, 13 insertions, 73 deletions
diff --git a/sbin/iked/timer.c b/sbin/iked/timer.c index 244900661be..916b79d1fe8 100644 --- a/sbin/iked/timer.c +++ b/sbin/iked/timer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.c,v 1.5 2011/05/27 12:01:02 reyk Exp $ */ +/* $OpenBSD: timer.c,v 1.6 2012/05/29 15:09:12 mikeb Exp $ */ /* * Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net> @@ -32,86 +32,26 @@ #include "iked.h" -struct timer_cbarg { - int tmr_active; - struct event tmr_ev; - struct iked *tmr_env; - struct timeval tmr_first; - struct timeval tmr_last; - struct timeval tmr_tv; - int (*tmr_initcb)(struct iked *, struct iked_policy *); -} timer_initiator; - -void timer_initiator_cb(int, short, void *); - -#define IKED_TIMER_INITIATOR_INITIAL 2 -#define IKED_TIMER_INITIATOR_INTERVAL 60 +void timer_callback(int, short, void *); void -timer_register_initiator(struct iked *env, - int (*cb)(struct iked *, struct iked_policy *)) +timer_register(struct iked_timer *tmr, struct iked *env, + void (*cb)(struct iked *, void *), void *arg, int timeout) { - struct timer_cbarg *tmr; - - timer_unregister_initiator(env); - - if (env->sc_passive) - return; - - tmr = &timer_initiator; - gettimeofday(&tmr->tmr_first, NULL); - gettimeofday(&tmr->tmr_last, NULL); + struct timeval tv = { timeout }; tmr->tmr_env = env; - tmr->tmr_initcb = cb; - tmr->tmr_active = 1; - evtimer_set(&tmr->tmr_ev, timer_initiator_cb, tmr); - - tmr->tmr_tv.tv_sec = IKED_TIMER_INITIATOR_INITIAL; - tmr->tmr_tv.tv_usec = 0; - evtimer_add(&tmr->tmr_ev, &tmr->tmr_tv); -} - -void -timer_unregister_initiator(struct iked *env) -{ - struct timer_cbarg *tmr; - - tmr = &timer_initiator; - if (!tmr->tmr_active) - return; - - event_del(&tmr->tmr_ev); - bzero(tmr, sizeof(*tmr)); + tmr->tmr_cb = cb; + tmr->tmr_cbarg = arg; + evtimer_set(&tmr->tmr_ev, timer_callback, tmr); + evtimer_add(&tmr->tmr_ev, &tv); } void -timer_initiator_cb(int fd, short event, void *arg) +timer_callback(int fd, short event, void *arg) { - struct timer_cbarg *tmr = arg; - struct iked *env = tmr->tmr_env; - struct iked_policy *pol; - - gettimeofday(&tmr->tmr_last, NULL); - - TAILQ_FOREACH(pol, &env->sc_policies, pol_entry) { - if ((pol->pol_flags & IKED_POLICY_ACTIVE) == 0) - continue; - if (sa_peer_lookup(pol, &pol->pol_peer.addr) != NULL) { - log_debug("%s: \"%s\" is already active", - __func__, pol->pol_name); - continue; - } - - log_debug("%s: initiating \"%s\"", __func__, pol->pol_name); - - if (tmr->tmr_initcb != NULL) { - /* Ignore error but what should we do on failure? */ - (void)tmr->tmr_initcb(env, pol); - } - } + struct iked_timer *tmr = arg; - tmr->tmr_tv.tv_sec = IKED_TIMER_INITIATOR_INTERVAL; - tmr->tmr_tv.tv_usec = 0; - evtimer_add(&tmr->tmr_ev, &tmr->tmr_tv); + if (tmr->tmr_cb) + tmr->tmr_cb(tmr->tmr_env, tmr->tmr_cbarg); } |