Home | History | Annotate | Line # | Download | only in rtadvd
timer.c revision 1.3
      1 /*	$NetBSD: timer.c,v 1.3 2000/05/23 11:37:59 itojun Exp $	*/
      2 /*	$KAME: timer.c,v 1.3 2000/05/22 22:23:07 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/time.h>
     34 
     35 #include <unistd.h>
     36 #include <syslog.h>
     37 #include <stdlib.h>
     38 #include <string.h>
     39 #if defined(__NetBSD__) || defined(__OpenBSD__)
     40 #include <search.h>
     41 #endif
     42 #include "timer.h"
     43 
     44 static struct rtadvd_timer timer_head;
     45 
     46 #define MILLION 1000000
     47 
     48 static struct timeval tm_max = {0x7fffffff, 0x7fffffff};
     49 
     50 void
     51 rtadvd_timer_init()
     52 {
     53 	memset(&timer_head, 0, sizeof(timer_head));
     54 
     55 	timer_head.next = timer_head.prev = &timer_head;
     56 	timer_head.tm = tm_max;
     57 }
     58 
     59 struct rtadvd_timer *
     60 rtadvd_add_timer(void (*timeout) __P((void *)),
     61 		void (*update) __P((void *, struct timeval *)),
     62 		 void *timeodata, void *updatedata)
     63 {
     64 	struct rtadvd_timer *newtimer;
     65 
     66 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
     67 		syslog(LOG_ERR,
     68 		       "<%s> can't allocate memory", __FUNCTION__);
     69 		exit(1);
     70 	}
     71 
     72 	memset(newtimer, 0, sizeof(*newtimer));
     73 
     74 	if (timeout == NULL) {
     75 		syslog(LOG_ERR,
     76 		       "<%s> timeout function unspecfied", __FUNCTION__);
     77 		exit(1);
     78 	}
     79 	if (update == NULL) {
     80 		syslog(LOG_ERR,
     81 		       "<%s> update function unspecfied", __FUNCTION__);
     82 		exit(1);
     83 	}
     84 	newtimer->expire = timeout;
     85 	newtimer->update = update;
     86 	newtimer->expire_data = timeodata;
     87 	newtimer->update_data = updatedata;
     88 	newtimer->tm = tm_max;
     89 
     90 	/* link into chain */
     91 	insque(newtimer, &timer_head);
     92 
     93 	return(newtimer);
     94 }
     95 
     96 void
     97 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer)
     98 {
     99 	struct timeval now;
    100 
    101 	/* reset the timer */
    102 	gettimeofday(&now, NULL);
    103 
    104 	TIMEVAL_ADD(&now, tm, &timer->tm);
    105 
    106 	/* update the next expiration time */
    107 	if (TIMEVAL_LT(timer->tm, timer_head.tm))
    108 		timer_head.tm = timer->tm;
    109 
    110 	return;
    111 }
    112 
    113 /*
    114  * Check expiration for each timer. If a timer is expired,
    115  * call the expire function for the timer and update the timer.
    116  * Return the next interval for select() call.
    117  */
    118 struct timeval *
    119 rtadvd_check_timer()
    120 {
    121 	static struct timeval returnval;
    122 	struct timeval now;
    123 	struct rtadvd_timer *tm = timer_head.next;
    124 
    125 	gettimeofday(&now, NULL);
    126 
    127 	timer_head.tm = tm_max;
    128 
    129 	while(tm != &timer_head) {
    130 		if (TIMEVAL_LEQ(tm->tm, now)) {
    131 			(*tm->expire)(tm->expire_data);
    132 			(*tm->update)(tm->update_data, &tm->tm);
    133 			TIMEVAL_ADD(&tm->tm, &now, &tm->tm);
    134 		}
    135 
    136 		if (TIMEVAL_LT(tm->tm, timer_head.tm))
    137 			timer_head.tm = tm->tm;
    138 
    139 		tm = tm->next;
    140 	}
    141 
    142 	if (TIMEVAL_LT(timer_head.tm, now)) {
    143 		/* this may occur when the interval is too small */
    144 		returnval.tv_sec = returnval.tv_usec = 0;
    145 	}
    146 	else
    147 		TIMEVAL_SUB(&timer_head.tm, &now, &returnval);
    148 	return(&returnval);
    149 }
    150 
    151 struct timeval *
    152 rtadvd_timer_rest(struct rtadvd_timer *timer)
    153 {
    154 	static struct timeval returnval, now;
    155 
    156 	gettimeofday(&now, NULL);
    157 	if (TIMEVAL_LEQ(timer->tm, now)) {
    158 		syslog(LOG_DEBUG,
    159 		       "<%s> a timer must be expired, but not yet",
    160 		       __FUNCTION__);
    161 		returnval.tv_sec = returnval.tv_usec = 0;
    162 	}
    163 	else
    164 		TIMEVAL_SUB(&timer->tm, &now, &returnval);
    165 
    166 	return(&returnval);
    167 }
    168 
    169 /* result = a + b */
    170 void
    171 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
    172 {
    173 	long l;
    174 
    175 	if ((l = a->tv_usec + b->tv_usec) < MILLION) {
    176 		result->tv_usec = l;
    177 		result->tv_sec = a->tv_sec + b->tv_sec;
    178 	}
    179 	else {
    180 		result->tv_usec = l - MILLION;
    181 		result->tv_sec = a->tv_sec + b->tv_sec + 1;
    182 	}
    183 }
    184 
    185 /*
    186  * result = a - b
    187  * XXX: this function assumes that a >= b.
    188  */
    189 void
    190 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
    191 {
    192 	long l;
    193 
    194 	if ((l = a->tv_usec - b->tv_usec) >= 0) {
    195 		result->tv_usec = l;
    196 		result->tv_sec = a->tv_sec - b->tv_sec;
    197 	}
    198 	else {
    199 		result->tv_usec = MILLION + l;
    200 		result->tv_sec = a->tv_sec - b->tv_sec - 1;
    201 	}
    202 }
    203