1 /* $FreeBSD: stable/10/usr.sbin/rtadvd/timer.c 253995 2013-08-06 15:49:18Z hrs $ */
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
34 #include <sys/queue.h>
35 #include <sys/socket.h>
38 #include <net/if_dl.h>
39 #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 timespec tm_limit
;
56 static struct timespec tm_max
;
59 rtadvd_timer_init(void)
61 /* Generate maximum time in timespec. */
62 tm_limit
.tv_sec
= (-1) & ~((time_t)1 << ((sizeof(tm_max
.tv_sec
) * 8) - 1));
63 tm_limit
.tv_nsec
= (-1) & ~((long)1 << ((sizeof(tm_max
.tv_nsec
) * 8) - 1));
65 TAILQ_INIT(&ra_timer
);
69 rtadvd_update_timeout_handler(void)
73 TAILQ_FOREACH(ifi
, &ifilist
, ifi_next
) {
74 switch (ifi
->ifi_state
) {
75 case IFI_STATE_CONFIGURED
:
76 case IFI_STATE_TRANSITIVE
:
77 if (ifi
->ifi_ra_timer
!= NULL
)
80 syslog(LOG_DEBUG
, "<%s> add timer for %s (idx=%d)",
81 __func__
, ifi
->ifi_ifname
, ifi
->ifi_ifindex
);
82 ifi
->ifi_ra_timer
= rtadvd_add_timer(ra_timeout
,
83 ra_timer_update
, ifi
, ifi
);
84 ra_timer_update((void *)ifi
,
85 &ifi
->ifi_ra_timer
->rat_tm
);
86 rtadvd_set_timer(&ifi
->ifi_ra_timer
->rat_tm
,
89 case IFI_STATE_UNCONFIGURED
:
90 if (ifi
->ifi_ra_timer
== NULL
)
94 "<%s> remove timer for %s (idx=%d)", __func__
,
95 ifi
->ifi_ifname
, ifi
->ifi_ifindex
);
96 rtadvd_remove_timer(ifi
->ifi_ra_timer
);
97 ifi
->ifi_ra_timer
= NULL
;
105 struct rtadvd_timer
*
106 rtadvd_add_timer(struct rtadvd_timer
*(*timeout
)(void *),
107 void (*update
)(void *, struct timespec
*),
108 void *timeodata
, void *updatedata
)
110 struct rtadvd_timer
*rat
;
112 if (timeout
== NULL
) {
114 "<%s> timeout function unspecified", __func__
);
118 rat
= malloc(sizeof(*rat
));
121 "<%s> can't allocate memory", __func__
);
124 memset(rat
, 0, sizeof(*rat
));
126 rat
->rat_expire
= timeout
;
127 rat
->rat_update
= update
;
128 rat
->rat_expire_data
= timeodata
;
129 rat
->rat_update_data
= updatedata
;
130 rat
->rat_tm
= tm_max
;
132 /* link into chain */
133 TAILQ_INSERT_TAIL(&ra_timer
, rat
, rat_next
);
139 rtadvd_remove_timer(struct rtadvd_timer
*rat
)
145 TAILQ_REMOVE(&ra_timer
, rat
, rat_next
);
150 * Check expiration for each timer. If a timer expires,
151 * call the expire function for the timer and update the timer.
152 * Return the next interval for select() call.
155 rtadvd_check_timer(void)
157 static struct timespec returnval
;
159 struct rtadvd_timer
*rat
;
161 clock_gettime(CLOCK_MONOTONIC_FAST
, &now
);
163 TAILQ_FOREACH(rat
, &ra_timer
, rat_next
) {
164 if (TS_CMP(&rat
->rat_tm
, &now
, <=)) {
165 if (((*rat
->rat_expire
)(rat
->rat_expire_data
) == NULL
))
166 continue; /* the timer was removed */
168 (*rat
->rat_update
)(rat
->rat_update_data
, &rat
->rat_tm
);
169 TS_ADD(&rat
->rat_tm
, &now
, &rat
->rat_tm
);
171 if (TS_CMP(&rat
->rat_tm
, &tm_max
, <))
172 tm_max
= rat
->rat_tm
;
174 if (TS_CMP(&tm_max
, &tm_limit
, ==)) {
175 /* no need to timeout */
177 } else if (TS_CMP(&tm_max
, &now
, <)) {
178 /* this may occur when the interval is too small */
179 returnval
.tv_sec
= returnval
.tv_nsec
= 0;
181 TS_SUB(&tm_max
, &now
, &returnval
);
186 rtadvd_set_timer(struct timespec
*tm
, struct rtadvd_timer
*rat
)
190 /* reset the timer */
191 clock_gettime(CLOCK_MONOTONIC_FAST
, &now
);
192 TS_ADD(&now
, tm
, &rat
->rat_tm
);
194 /* update the next expiration time */
195 if (TS_CMP(&rat
->rat_tm
, &tm_max
, <))
196 tm_max
= rat
->rat_tm
;