]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/rtadvd/timer.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / rtadvd / timer.c
1 /*      $FreeBSD$       */
2 /*      $KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $   */
3
4 /*
5  * Copyright (C) 1998 WIDE Project.
6  * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/time.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
37
38 #include <net/if.h>
39 #include <net/if_dl.h>
40 #include <netinet/in.h>
41
42 #include <unistd.h>
43 #include <syslog.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <search.h>
47 #include <netdb.h>
48
49 #include "rtadvd.h"
50 #include "timer_subr.h"
51 #include "timer.h"
52
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;
57
58 void
59 rtadvd_timer_init(void)
60 {
61
62         tm_max = tm_limit;
63         TAILQ_INIT(&ra_timer);
64 }
65
66 void
67 rtadvd_update_timeout_handler(void)
68 {
69         struct ifinfo *ifi;
70
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)
76                                 continue;
77
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,
85                             ifi->ifi_ra_timer);
86                         break;
87                 case IFI_STATE_UNCONFIGURED:
88                         if (ifi->ifi_ra_timer == NULL)
89                                 continue;
90
91                         syslog(LOG_DEBUG,
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;
96                         break;
97                 }
98         }
99
100         return;
101 }
102
103 struct rtadvd_timer *
104 rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
105     void (*update)(void *, struct timeval *),
106     void *timeodata, void *updatedata)
107 {
108         struct rtadvd_timer *rat;
109
110         if (timeout == NULL) {
111                 syslog(LOG_ERR,
112                     "<%s> timeout function unspecified", __func__);
113                 exit(1);
114         }
115
116         rat = malloc(sizeof(*rat));
117         if (rat == NULL) {
118                 syslog(LOG_ERR,
119                     "<%s> can't allocate memory", __func__);
120                 exit(1);
121         }
122         memset(rat, 0, sizeof(*rat));
123
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;
129
130         /* link into chain */
131         TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
132
133         return (rat);
134 }
135
136 void
137 rtadvd_remove_timer(struct rtadvd_timer *rat)
138 {
139
140         if (rat == NULL)
141                 return;
142
143         TAILQ_REMOVE(&ra_timer, rat, rat_next);
144         free(rat);
145 }
146
147 /*
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.
151  */
152 struct timeval *
153 rtadvd_check_timer(void)
154 {
155         static struct timeval returnval;
156         struct timeval now;
157         struct rtadvd_timer *rat;
158
159         gettimeofday(&now, NULL);
160         tm_max = tm_limit;
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 */
165                         if (rat->rat_update)
166                                 (*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
167                         TIMEVAL_ADD(&rat->rat_tm, &now, &rat->rat_tm);
168                 }
169                 if (TIMEVAL_LT(&rat->rat_tm, &tm_max))
170                         tm_max = rat->rat_tm;
171         }
172         if (TIMEVAL_EQUAL(&tm_max, &tm_limit)) {
173                 /* no need to timeout */
174                 return (NULL);
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;
178         } else
179                 TIMEVAL_SUB(&tm_max, &now, &returnval);
180         return (&returnval);
181 }
182
183 void
184 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *rat)
185 {
186         struct timeval now;
187
188         /* reset the timer */
189         gettimeofday(&now, NULL);
190         TIMEVAL_ADD(&now, tm, &rat->rat_tm);
191
192         /* update the next expiration time */
193         if (TIMEVAL_LT(&rat->rat_tm, &tm_max))
194                 tm_max = rat->rat_tm;
195
196         return;
197 }