1 /*	$KAME: timer.c,v 1.6 2003/07/31 23:25:59 jinmei Exp $	*/
2 
3 /*
4  * Copyright (C) 2002 WIDE Project.
5  * All rights reserved.
6  *
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.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <sys/queue.h>
35 
36 #include <netinet/in.h>
37 
38 #include <unistd.h>
39 #include <syslog.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #if defined(__NetBSD__) || defined(__OpenBSD__)
43 #include <search.h>
44 #endif
45 #include "dhcp6.h"
46 #include "config.h"
47 #include "common.h"
48 #include "timer.h"
49 
50 #define MILLION 1000000
51 
52 LIST_HEAD(, dhcp6_timer) timer_head;
53 static struct timeval tm_sentinel;
54 static struct timeval tm_max = {0x7fffffff, 0x7fffffff};
55 
56 static void timeval_add __P((struct timeval *, struct timeval *,
57 			     struct timeval *));
58 
59 void
dhcp6_timer_init()60 dhcp6_timer_init()
61 {
62 	LIST_INIT(&timer_head);
63 	tm_sentinel = tm_max;
64 }
65 
66 struct dhcp6_timer *
67 dhcp6_add_timer(timeout, timeodata)
68 	struct dhcp6_timer *(*timeout) __P((void *));
69 	void *timeodata;
70 {
71 	struct dhcp6_timer *newtimer;
72 
73 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
74 		d_printf(LOG_ERR, FNAME, "can't allocate memory");
75 		return (NULL);
76 	}
77 
78 	memset(newtimer, 0, sizeof(*newtimer));
79 
80 	if (timeout == NULL) {
81 		d_printf(LOG_ERR, FNAME, "timeout function unspecified");
82 		exit(1);
83 	}
84 	newtimer->expire = timeout;
85 	newtimer->expire_data = timeodata;
86 	newtimer->tm = tm_max;
87 
88 	LIST_INSERT_HEAD(&timer_head, newtimer, link);
89 
90 	return (newtimer);
91 }
92 
93 void
dhcp6_remove_timer(timer)94 dhcp6_remove_timer(timer)
95 	struct dhcp6_timer **timer;
96 {
97 	LIST_REMOVE(*timer, link);
98 	free(*timer);
99 	*timer = NULL;
100 }
101 
102 void
dhcp6_set_timer(tm,timer)103 dhcp6_set_timer(tm, timer)
104 	struct timeval *tm;
105 	struct dhcp6_timer *timer;
106 {
107 	struct timeval now;
108 
109 	/* reset the timer */
110 	gettimeofday(&now, NULL);
111 
112 	timeval_add(&now, tm, &timer->tm);
113 
114 	/* update the next expiration time */
115 	if (TIMEVAL_LT(timer->tm, tm_sentinel))
116 		tm_sentinel = timer->tm;
117 
118 	return;
119 }
120 
121 /*
122  * Check expiration for each timer. If a timer is expired,
123  * call the expire function for the timer and update the timer.
124  * Return the next interval for select() call.
125  */
126 struct timeval *
dhcp6_check_timer()127 dhcp6_check_timer()
128 {
129 	static struct timeval returnval;
130 	struct timeval now;
131 	struct dhcp6_timer *tm, *tm_next;
132 
133 	gettimeofday(&now, NULL);
134 
135 	tm_sentinel = tm_max;
136 	for (tm = LIST_FIRST(&timer_head); tm; tm = tm_next) {
137 		tm_next = LIST_NEXT(tm, link);
138 
139 		if (TIMEVAL_LEQ(tm->tm, now)) {
140 			if ((*tm->expire)(tm->expire_data) == NULL)
141 				continue; /* timer has been freed */
142 		}
143 
144 		if (TIMEVAL_LT(tm->tm, tm_sentinel))
145 			tm_sentinel = tm->tm;
146 	}
147 
148 	if (TIMEVAL_EQUAL(tm_max, tm_sentinel)) {
149 		/* no need to timeout */
150 		return (NULL);
151 	} else if (TIMEVAL_LT(tm_sentinel, now)) {
152 		/* this may occur when the interval is too small */
153 		returnval.tv_sec = returnval.tv_usec = 0;
154 	} else
155 		timeval_sub(&tm_sentinel, &now, &returnval);
156 	return (&returnval);
157 }
158 
159 struct timeval *
dhcp6_timer_rest(timer)160 dhcp6_timer_rest(timer)
161 	struct dhcp6_timer *timer;
162 {
163 	struct timeval now;
164 	static struct timeval returnval; /* XXX */
165 
166 	gettimeofday(&now, NULL);
167 	if (TIMEVAL_LEQ(timer->tm, now)) {
168 		d_printf(LOG_DEBUG, FNAME,
169 		    "a timer must be expired, but not yet");
170 		returnval.tv_sec = returnval.tv_usec = 0;
171 	} else
172 		timeval_sub(&timer->tm, &now, &returnval);
173 
174 	return (&returnval);
175 }
176 
177 /* result = a + b */
178 static void
timeval_add(a,b,result)179 timeval_add(a, b, result)
180 	struct timeval *a, *b, *result;
181 {
182 	long l;
183 
184 	if ((l = a->tv_usec + b->tv_usec) < MILLION) {
185 		result->tv_usec = l;
186 		result->tv_sec = a->tv_sec + b->tv_sec;
187 	}
188 	else {
189 		result->tv_usec = l - MILLION;
190 		result->tv_sec = a->tv_sec + b->tv_sec + 1;
191 	}
192 }
193 
194 /*
195  * result = a - b
196  * XXX: this function assumes that a >= b.
197  */
198 void
timeval_sub(a,b,result)199 timeval_sub(a, b, result)
200 	struct timeval *a, *b, *result;
201 {
202 	long l;
203 
204 	if ((l = a->tv_usec - b->tv_usec) >= 0) {
205 		result->tv_usec = l;
206 		result->tv_sec = a->tv_sec - b->tv_sec;
207 	}
208 	else {
209 		result->tv_usec = MILLION + l;
210 		result->tv_sec = a->tv_sec - b->tv_sec - 1;
211 	}
212 }
213