]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/rtadvd/timer.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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/queue.h>
35 #include <sys/socket.h>
36
37 #include <net/if.h>
38 #include <net/if_dl.h>
39 #include <netinet/in.h>
40
41 #include <unistd.h>
42 #include <syslog.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <search.h>
46 #include <time.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 timespec tm_limit;
56 static struct timespec tm_max;
57
58 void
59 rtadvd_timer_init(void)
60 {
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));
64         tm_max = tm_limit;
65         TAILQ_INIT(&ra_timer);
66 }
67
68 void
69 rtadvd_update_timeout_handler(void)
70 {
71         struct ifinfo *ifi;
72
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)
78                                 continue;
79
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,
87                             ifi->ifi_ra_timer);
88                         break;
89                 case IFI_STATE_UNCONFIGURED:
90                         if (ifi->ifi_ra_timer == NULL)
91                                 continue;
92
93                         syslog(LOG_DEBUG,
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;
98                         break;
99                 }
100         }
101
102         return;
103 }
104
105 struct rtadvd_timer *
106 rtadvd_add_timer(struct rtadvd_timer *(*timeout)(void *),
107     void (*update)(void *, struct timespec *),
108     void *timeodata, void *updatedata)
109 {
110         struct rtadvd_timer *rat;
111
112         if (timeout == NULL) {
113                 syslog(LOG_ERR,
114                     "<%s> timeout function unspecified", __func__);
115                 exit(1);
116         }
117
118         rat = malloc(sizeof(*rat));
119         if (rat == NULL) {
120                 syslog(LOG_ERR,
121                     "<%s> can't allocate memory", __func__);
122                 exit(1);
123         }
124         memset(rat, 0, sizeof(*rat));
125
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;
131
132         /* link into chain */
133         TAILQ_INSERT_TAIL(&ra_timer, rat, rat_next);
134
135         return (rat);
136 }
137
138 void
139 rtadvd_remove_timer(struct rtadvd_timer *rat)
140 {
141
142         if (rat == NULL)
143                 return;
144
145         TAILQ_REMOVE(&ra_timer, rat, rat_next);
146         free(rat);
147 }
148
149 /*
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.
153  */
154 struct timespec *
155 rtadvd_check_timer(void)
156 {
157         static struct timespec returnval;
158         struct timespec now;
159         struct rtadvd_timer *rat;
160
161         clock_gettime(CLOCK_MONOTONIC_FAST, &now);
162         tm_max = tm_limit;
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 */
167                         if (rat->rat_update)
168                                 (*rat->rat_update)(rat->rat_update_data, &rat->rat_tm);
169                         TS_ADD(&rat->rat_tm, &now, &rat->rat_tm);
170                 }
171                 if (TS_CMP(&rat->rat_tm, &tm_max, <))
172                         tm_max = rat->rat_tm;
173         }
174         if (TS_CMP(&tm_max, &tm_limit, ==)) {
175                 /* no need to timeout */
176                 return (NULL);
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;
180         } else
181                 TS_SUB(&tm_max, &now, &returnval);
182         return (&returnval);
183 }
184
185 void
186 rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *rat)
187 {
188         struct timespec now;
189
190         /* reset the timer */
191         clock_gettime(CLOCK_MONOTONIC_FAST, &now);
192         TS_ADD(&now, tm, &rat->rat_tm);
193
194         /* update the next expiration time */
195         if (TS_CMP(&rat->rat_tm, &tm_max, <))
196                 tm_max = rat->rat_tm;
197
198         return;
199 }