xref: /freebsd/usr.sbin/rtadvd/timer.c (revision 6780ab54325a71e7e70112b11657973edde8655e)
1 /*	$FreeBSD$	*/
2 /*	$KAME: timer.c,v 1.4 2000/05/27 11:30:43 jinmei Exp $	*/
3 
4 /*
5  * Copyright (C) 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/time.h>
34 
35 #include <unistd.h>
36 #include <syslog.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #if defined(__NetBSD__) || defined(__OpenBSD__)
40 #include <search.h>
41 #endif
42 #include "timer.h"
43 
44 static struct rtadvd_timer timer_head;
45 
46 #define MILLION 1000000
47 #define TIMEVAL_EQUAL(t1,t2) ((t1)->tv_sec == (t2)->tv_sec &&\
48  (t1)->tv_usec == (t2)->tv_usec)
49 
50 static struct timeval tm_max = {0x7fffffff, 0x7fffffff};
51 
52 void
53 rtadvd_timer_init()
54 {
55 	memset(&timer_head, 0, sizeof(timer_head));
56 
57 	timer_head.next = timer_head.prev = &timer_head;
58 	timer_head.tm = tm_max;
59 }
60 
61 struct rtadvd_timer *
62 rtadvd_add_timer(struct rtadvd_timer *(*timeout) __P((void *)),
63     void (*update) __P((void *, struct timeval *)),
64     void *timeodata, void *updatedata)
65 {
66 	struct rtadvd_timer *newtimer;
67 
68 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
69 		syslog(LOG_ERR,
70 		       "<%s> can't allocate memory", __FUNCTION__);
71 		exit(1);
72 	}
73 
74 	memset(newtimer, 0, sizeof(*newtimer));
75 
76 	if (timeout == NULL) {
77 		syslog(LOG_ERR,
78 		       "<%s> timeout function unspecfied", __FUNCTION__);
79 		exit(1);
80 	}
81 	newtimer->expire = timeout;
82 	newtimer->update = update;
83 	newtimer->expire_data = timeodata;
84 	newtimer->update_data = updatedata;
85 	newtimer->tm = tm_max;
86 
87 	/* link into chain */
88 	insque(newtimer, &timer_head);
89 
90 	return(newtimer);
91 }
92 
93 void
94 rtadvd_remove_timer(struct rtadvd_timer **timer)
95 {
96 	remque(*timer);
97 	free(*timer);
98 	*timer = NULL;
99 }
100 
101 void
102 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer)
103 {
104 	struct timeval now;
105 
106 	/* reset the timer */
107 	gettimeofday(&now, NULL);
108 
109 	TIMEVAL_ADD(&now, tm, &timer->tm);
110 
111 	/* update the next expiration time */
112 	if (TIMEVAL_LT(timer->tm, timer_head.tm))
113 		timer_head.tm = timer->tm;
114 
115 	return;
116 }
117 
118 /*
119  * Check expiration for each timer. If a timer expires,
120  * call the expire function for the timer and update the timer.
121  * Return the next interval for select() call.
122  */
123 struct timeval *
124 rtadvd_check_timer()
125 {
126 	static struct timeval returnval;
127 	struct timeval now;
128 	struct rtadvd_timer *tm = timer_head.next, *tm_next;
129 
130 	gettimeofday(&now, NULL);
131 
132 	timer_head.tm = tm_max;
133 
134 	for (tm = timer_head.next; tm != &timer_head; tm = tm_next) {
135 		tm_next = tm->next;
136 
137 		if (TIMEVAL_LEQ(tm->tm, now)) {
138 			if (((*tm->expire)(tm->expire_data) == NULL))
139 				continue; /* the timer was removed */
140 			if (tm->update)
141 				(*tm->update)(tm->update_data, &tm->tm);
142 			TIMEVAL_ADD(&tm->tm, &now, &tm->tm);
143 		}
144 
145 		if (TIMEVAL_LT(tm->tm, timer_head.tm))
146 			timer_head.tm = tm->tm;
147 	}
148 
149 	if (TIMEVAL_EQUAL(&tm_max, &timer_head.tm)) {
150 		/* no need to timeout */
151 		return(NULL);
152 	}
153 	else if (TIMEVAL_LT(timer_head.tm, now)) {
154 		/* this may occur when the interval is too small */
155 		returnval.tv_sec = returnval.tv_usec = 0;
156 	}
157 	else
158 		TIMEVAL_SUB(&timer_head.tm, &now, &returnval);
159 	return(&returnval);
160 }
161 
162 struct timeval *
163 rtadvd_timer_rest(struct rtadvd_timer *timer)
164 {
165 	static struct timeval returnval, now;
166 
167 	gettimeofday(&now, NULL);
168 	if (TIMEVAL_LEQ(timer->tm, now)) {
169 		syslog(LOG_DEBUG,
170 		       "<%s> a timer must be expired, but not yet",
171 		       __FUNCTION__);
172 		returnval.tv_sec = returnval.tv_usec = 0;
173 	}
174 	else
175 		TIMEVAL_SUB(&timer->tm, &now, &returnval);
176 
177 	return(&returnval);
178 }
179 
180 /* result = a + b */
181 void
182 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
183 {
184 	long l;
185 
186 	if ((l = a->tv_usec + b->tv_usec) < MILLION) {
187 		result->tv_usec = l;
188 		result->tv_sec = a->tv_sec + b->tv_sec;
189 	}
190 	else {
191 		result->tv_usec = l - MILLION;
192 		result->tv_sec = a->tv_sec + b->tv_sec + 1;
193 	}
194 }
195 
196 /*
197  * result = a - b
198  * XXX: this function assumes that a >= b.
199  */
200 void
201 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
202 {
203 	long l;
204 
205 	if ((l = a->tv_usec - b->tv_usec) >= 0) {
206 		result->tv_usec = l;
207 		result->tv_sec = a->tv_sec - b->tv_sec;
208 	}
209 	else {
210 		result->tv_usec = MILLION + l;
211 		result->tv_sec = a->tv_sec - b->tv_sec - 1;
212 	}
213 }
214