adden new isakmpd
[anytun.git] / keyexchange / isakmpd-20041012 / timer.c
blob45bcc49e4aece9d3e3b1408f5d1fc52f4edc2f80
1 /* $OpenBSD: timer.c,v 1.14 2004/06/14 09:55:42 ho Exp $ */
2 /* $EOM: timer.c,v 1.13 2000/02/20 19:58:42 niklas Exp $ */
4 /*
5 * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This code was written under funding by Ericsson Radio Systems.
32 #include <sys/queue.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "sysdep.h"
38 #include "log.h"
39 #include "timer.h"
41 static TAILQ_HEAD(event_list, event) events;
43 void
44 timer_init(void)
46 TAILQ_INIT(&events);
49 void
50 timer_next_event(struct timeval **timeout)
52 struct timeval now;
54 if (TAILQ_FIRST(&events)) {
55 gettimeofday(&now, 0);
56 if (timercmp(&now, &TAILQ_FIRST(&events)->expiration, >=))
57 timerclear(*timeout);
58 else
59 timersub(&TAILQ_FIRST(&events)->expiration, &now,
60 *timeout);
61 } else
62 *timeout = 0;
65 void
66 timer_handle_expirations(void)
68 struct timeval now;
69 struct event *n;
71 gettimeofday(&now, 0);
72 for (n = TAILQ_FIRST(&events); n && timercmp(&now, &n->expiration, >=);
73 n = TAILQ_FIRST(&events)) {
74 LOG_DBG((LOG_TIMER, 10,
75 "timer_handle_expirations: event %s(%p)", n->name,
76 n->arg));
77 TAILQ_REMOVE(&events, n, link);
78 (*n->func)(n->arg);
79 free(n);
83 struct event *
84 timer_add_event(char *name, void (*func)(void *), void *arg,
85 struct timeval *expiration)
87 struct event *ev = (struct event *) malloc(sizeof *ev);
88 struct event *n;
89 struct timeval now;
91 if (!ev)
92 return 0;
93 ev->name = name;
94 ev->func = func;
95 ev->arg = arg;
96 gettimeofday(&now, 0);
97 memcpy(&ev->expiration, expiration, sizeof *expiration);
98 for (n = TAILQ_FIRST(&events);
99 n && timercmp(expiration, &n->expiration, >=);
100 n = TAILQ_NEXT(n, link))
102 if (n) {
103 LOG_DBG((LOG_TIMER, 10,
104 "timer_add_event: event %s(%p) added before %s(%p), "
105 "expiration in %lds", name,
106 arg, n->name, n->arg, expiration->tv_sec - now.tv_sec));
107 TAILQ_INSERT_BEFORE(n, ev, link);
108 } else {
109 LOG_DBG((LOG_TIMER, 10, "timer_add_event: event %s(%p) added "
110 "last, expiration in %lds", name, arg,
111 expiration->tv_sec - now.tv_sec));
112 TAILQ_INSERT_TAIL(&events, ev, link);
114 return ev;
117 void
118 timer_remove_event(struct event *ev)
120 LOG_DBG((LOG_TIMER, 10, "timer_remove_event: removing event %s(%p)",
121 ev->name, ev->arg));
122 TAILQ_REMOVE(&events, ev, link);
123 free(ev);
126 void
127 timer_report(void)
129 struct event *ev;
130 struct timeval now;
132 gettimeofday(&now, 0);
134 for (ev = TAILQ_FIRST(&events); ev; ev = TAILQ_NEXT(ev, link))
135 LOG_DBG((LOG_REPORT, 0,
136 "timer_report: event %s(%p) scheduled in %d seconds",
137 (ev->name ? ev->name : "<unknown>"), ev,
138 (int) (ev->expiration.tv_sec - now.tv_sec)));