2 /* $KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/queue.h>
36 #include <sys/socket.h>
39 #include <net/if_dl.h>
40 #include <netinet/in.h>
50 #include "timer_subr.h"
53 struct rtadvd_timer_head_t ra_timer =
54 TAILQ_HEAD_INITIALIZER(ra_timer);
55 static struct timeval tm_limit = {0x7fffffff, 0x7fffffff};
56 static struct timeval tm_max;
59 rtadvd_timer_init(void)
63 TAILQ_INIT(&ra_timer);
67 rtadvd_update_timeout_handler(void)
71 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
72 switch (ifi->ifi_state) {
73 case IFI_STATE_CONFIGURED:
74 case IFI_STATE_TRANSITIVE:
75 if (ifi->ifi_ra_timer != NULL)
78 syslog(LOG_DEBUG, "<%s> add timer for %s (idx=%d)",
79 __func__, ifi->ifi_ifname, ifi->ifi_ifindex);
80 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
81 ra_timer_update, ifi, ifi);
82 ra_timer_update((void *)ifi,
83 &ifi->ifi_ra_timer->rat_tm);
84 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
87 case IFI_STATE_UNCONFIGURED:
88 if (ifi->ifi_ra_timer == NULL)
92 "<%s> remove timer for %s (idx=%d)", __func__,
93 ifi->ifi_ifname, ifi->ifi_ifindex);
94 rtadvd_remove_timer(ifi->ifi_ra_timer);
95 ifi->ifi_ra_timer = NULL;
103 struct rtadvd_timer *
104 rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
105 void (*update)(void *, struct timeval *),
106 void *timeodata, void *updatedata)
108 struct rtadvd_timer *rat;
110 if (timeout == NULL) {
112 "<%s> timeout function unspecified", __func__);
116 rat = malloc(sizeof(*rat));
119 "<%s> can't allocate memory", __func__);
122 memset(rat, 0, sizeof(*rat));
124 rat->rat_expire = timeout;
125 rat->rat_update = update;
126 rat->rat_expire_data = timeodata;
127 rat->rat_update_data = updatedata;
128 rat->rat_tm = tm_max;
130 /* link into chain */
131 TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
137 rtadvd_remove_timer(struct rtadvd_timer *rat)
143 TAILQ_REMOVE(&ra_timer, rat, rat_next);
148 * Check expiration for each timer. If a timer expires,
149 * call the expire function for the timer and update the timer.
150 * Return the next interval for select() call.
153 rtadvd_check_timer(void)
155 static struct timeval returnval;
157 struct rtadvd_timer *rat;
159 gettimeofday(&now, NULL);
161 TAILQ_FOREACH(rat, &ra_timer, rat_next) {
162 if (TIMEVAL_LEQ(&rat->rat_tm, &now)) {
163 if (((*rat->rat_expire)(rat->rat_expire_data) == NULL))
164 continue; /* the timer was removed */
166 (*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
167 TIMEVAL_ADD(&rat->rat_tm, &now, &rat->rat_tm);
169 if (TIMEVAL_LT(&rat->rat_tm, &tm_max))
170 tm_max = rat->rat_tm;
172 if (TIMEVAL_EQUAL(&tm_max, &tm_limit)) {
173 /* no need to timeout */
175 } else if (TIMEVAL_LT(&tm_max, &now)) {
176 /* this may occur when the interval is too small */
177 returnval.tv_sec = returnval.tv_usec = 0;
179 TIMEVAL_SUB(&tm_max, &now, &returnval);
184 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *rat)
188 /* reset the timer */
189 gettimeofday(&now, NULL);
190 TIMEVAL_ADD(&now, tm, &rat->rat_tm);
192 /* update the next expiration time */
193 if (TIMEVAL_LT(&rat->rat_tm, &tm_max))
194 tm_max = rat->rat_tm;