diff options
author | 2013-04-30 12:30:40 +0000 | |
---|---|---|
committer | 2013-04-30 12:30:40 +0000 | |
commit | 779a0be13a4158086c06c3eef9c3c718e486ebae (patch) | |
tree | 0fe3260f8c420b63d12a399a3c6a4c973d38ddc5 | |
parent | move variables to type time_t; fix format strings (diff) | |
download | wireguard-openbsd-779a0be13a4158086c06c3eef9c3c718e486ebae.tar.xz wireguard-openbsd-779a0be13a4158086c06c3eef9c3c718e486ebae.zip |
- replace TIMEVAL_* function/macros with macros from sys/time.h
- replace insque(3) with SLIST from queue(3)
- with that, rewrite rtadvd_check_timer() logic to avoid
timeval {0x7fffffff, 0x7fffffff}
test/ok sthen@
-rw-r--r-- | usr.sbin/rtadvd/config.c | 3 | ||||
-rw-r--r-- | usr.sbin/rtadvd/rtadvd.c | 15 | ||||
-rw-r--r-- | usr.sbin/rtadvd/timer.c | 99 | ||||
-rw-r--r-- | usr.sbin/rtadvd/timer.h | 18 |
4 files changed, 29 insertions, 106 deletions
diff --git a/usr.sbin/rtadvd/config.c b/usr.sbin/rtadvd/config.c index 07abb62ce72..f6e22849280 100644 --- a/usr.sbin/rtadvd/config.c +++ b/usr.sbin/rtadvd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.32 2013/04/30 12:29:04 florian Exp $ */ +/* $OpenBSD: config.c,v 1.33 2013/04/30 12:30:40 florian Exp $ */ /* $KAME: config.c,v 1.62 2002/05/29 10:13:10 itojun Exp $ */ /* @@ -52,7 +52,6 @@ #include <errno.h> #include <string.h> #include <stdlib.h> -#include <search.h> #include <unistd.h> #include <ifaddrs.h> #include <stdint.h> diff --git a/usr.sbin/rtadvd/rtadvd.c b/usr.sbin/rtadvd/rtadvd.c index 45d96a23ca6..fd390fcb7a4 100644 --- a/usr.sbin/rtadvd/rtadvd.c +++ b/usr.sbin/rtadvd/rtadvd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rtadvd.c,v 1.43 2013/04/30 12:29:04 florian Exp $ */ +/* $OpenBSD: rtadvd.c,v 1.44 2013/04/30 12:30:40 florian Exp $ */ /* $KAME: rtadvd.c,v 1.66 2002/05/29 14:18:36 itojun Exp $ */ /* @@ -203,9 +203,6 @@ main(argc, argv) SLIST_INIT(&ralist); - /* timer initialization */ - rtadvd_timer_init(); - /* get iflist block from kernel */ init_iflist(); @@ -760,7 +757,7 @@ rs_input(int len, struct nd_router_solicit *rs, interval.tv_sec = 0; interval.tv_usec = delay; rest = rtadvd_timer_rest(ra->timer); - if (TIMEVAL_LT(*rest, interval)) { + if (timercmp(rest, &interval, <)) { log_debug("random delay is larger than " "the rest of normal timer"); interval = *rest; @@ -774,12 +771,12 @@ rs_input(int len, struct nd_router_solicit *rs, * previous advertisement was sent. */ gettimeofday(&now, NULL); - TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp); + timersub(&now, &ra->lastsent, &tm_tmp); min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS; min_delay.tv_usec = 0; - if (TIMEVAL_LT(tm_tmp, min_delay)) { - TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay); - TIMEVAL_ADD(&min_delay, &interval, &interval); + if (timercmp(&tm_tmp, &min_delay, <)) { + timersub(&min_delay, &tm_tmp, &min_delay); + timeradd(&min_delay, &interval, &interval); } rtadvd_set_timer(&interval, ra->timer); goto done; diff --git a/usr.sbin/rtadvd/timer.c b/usr.sbin/rtadvd/timer.c index d9ad519e4ca..249a50d181b 100644 --- a/usr.sbin/rtadvd/timer.c +++ b/usr.sbin/rtadvd/timer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.c,v 1.10 2011/03/22 10:16:23 okan Exp $ */ +/* $OpenBSD: timer.c,v 1.11 2013/04/30 12:30:40 florian Exp $ */ /* $KAME: timer.c,v 1.7 2002/05/21 14:26:55 itojun Exp $ */ /* @@ -30,31 +30,16 @@ * SUCH DAMAGE. */ +#include <sys/queue.h> #include <sys/time.h> #include <unistd.h> #include <stdlib.h> #include <string.h> -#include <search.h> #include "timer.h" #include "log.h" -static struct rtadvd_timer timer_head; - -#define MILLION 1000000 -#define TIMEVAL_EQUAL(t1,t2) ((t1)->tv_sec == (t2)->tv_sec &&\ - (t1)->tv_usec == (t2)->tv_usec) - -static struct timeval tm_max = {0x7fffffff, 0x7fffffff}; - -void -rtadvd_timer_init() -{ - memset(&timer_head, 0, sizeof(timer_head)); - - timer_head.next = timer_head.prev = &timer_head; - timer_head.tm = tm_max; -} +SLIST_HEAD(, rtadvd_timer) timer_head = SLIST_HEAD_INITIALIZER(timer_head); struct rtadvd_timer * rtadvd_add_timer(void (*timeout)(void *), @@ -76,10 +61,9 @@ rtadvd_add_timer(void (*timeout)(void *), newtimer->update = update; newtimer->expire_data = timeodata; newtimer->update_data = updatedata; - newtimer->tm = tm_max; /* link into chain */ - insque(newtimer, &timer_head); + SLIST_INSERT_HEAD(&timer_head, newtimer, entries); return(newtimer); } @@ -87,7 +71,7 @@ rtadvd_add_timer(void (*timeout)(void *), void rtadvd_remove_timer(struct rtadvd_timer **timer) { - remque(*timer); + SLIST_REMOVE(&timer_head, *timer, rtadvd_timer, entries); free(*timer); *timer = NULL; } @@ -100,13 +84,7 @@ rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer) /* reset the timer */ gettimeofday(&now, NULL); - TIMEVAL_ADD(&now, tm, &timer->tm); - - /* update the next expiration time */ - if (TIMEVAL_LT(timer->tm, timer_head.tm)) - timer_head.tm = timer->tm; - - return; + timeradd(&now, tm, &timer->tm); } /* @@ -119,33 +97,31 @@ rtadvd_check_timer() { static struct timeval returnval; struct timeval now; - struct rtadvd_timer *tm = timer_head.next; + struct rtadvd_timer *tm; + int timers; + timers = 0; gettimeofday(&now, NULL); - timer_head.tm = tm_max; - - while (tm != &timer_head) { - if (TIMEVAL_LEQ(tm->tm, now)) { + SLIST_FOREACH(tm, &timer_head, entries) { + if (timercmp(&tm->tm, &now, <=)) { (*tm->expire)(tm->expire_data); (*tm->update)(tm->update_data, &tm->tm); - TIMEVAL_ADD(&tm->tm, &now, &tm->tm); + timeradd(&tm->tm, &now, &tm->tm); } - - if (TIMEVAL_LT(tm->tm, timer_head.tm)) - timer_head.tm = tm->tm; - - tm = tm->next; + if (timers == 0 || timercmp(&tm->tm, &returnval, <)) + returnval = tm->tm; + timers ++; } - if (TIMEVAL_EQUAL(&tm_max, &timer_head.tm)) { + if (timers == 0) { /* no need to timeout */ return(NULL); - } else if (TIMEVAL_LT(timer_head.tm, now)) { + } else if (timercmp(&returnval, &now, <)) { /* this may occur when the interval is too small */ timerclear(&returnval); } else - TIMEVAL_SUB(&timer_head.tm, &now, &returnval); + timersub(&returnval, &now, &returnval); return(&returnval); } @@ -155,47 +131,12 @@ rtadvd_timer_rest(struct rtadvd_timer *timer) static struct timeval returnval, now; gettimeofday(&now, NULL); - if (TIMEVAL_LEQ(timer->tm, now)) { + if (timercmp(&timer->tm, &now, <=)) { log_debug("a timer must be expired, but not yet"); timerclear(&returnval); } else - TIMEVAL_SUB(&timer->tm, &now, &returnval); + timersub(&timer->tm, &now, &returnval); return(&returnval); } - -/* result = a + b */ -void -TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) -{ - long l; - - if ((l = a->tv_usec + b->tv_usec) < MILLION) { - result->tv_usec = l; - result->tv_sec = a->tv_sec + b->tv_sec; - } - else { - result->tv_usec = l - MILLION; - result->tv_sec = a->tv_sec + b->tv_sec + 1; - } -} - -/* - * result = a - b - * XXX: this function assumes that a >= b. - */ -void -TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) -{ - long l; - - if ((l = a->tv_usec - b->tv_usec) >= 0) { - result->tv_usec = l; - result->tv_sec = a->tv_sec - b->tv_sec; - } - else { - result->tv_usec = MILLION + l; - result->tv_sec = a->tv_sec - b->tv_sec - 1; - } -} diff --git a/usr.sbin/rtadvd/timer.h b/usr.sbin/rtadvd/timer.h index 3e8d751f2d5..20ffa8b195e 100644 --- a/usr.sbin/rtadvd/timer.h +++ b/usr.sbin/rtadvd/timer.h @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.h,v 1.6 2002/02/17 19:42:39 millert Exp $ */ +/* $OpenBSD: timer.h,v 1.7 2013/04/30 12:30:40 florian Exp $ */ /* $KAME: timer.h,v 1.3 2000/05/27 11:30:43 jinmei Exp $ */ /* @@ -30,19 +30,8 @@ * SUCH DAMAGE. */ -/* a < b */ -#define TIMEVAL_LT(a, b) (((a).tv_sec < (b).tv_sec) ||\ - (((a).tv_sec == (b).tv_sec) && \ - ((a).tv_usec < (b).tv_usec))) - -/* a <= b */ -#define TIMEVAL_LEQ(a, b) (((a).tv_sec < (b).tv_sec) ||\ - (((a).tv_sec == (b).tv_sec) &&\ - ((a).tv_usec <= (b).tv_usec))) - struct rtadvd_timer { - struct rtadvd_timer *next; - struct rtadvd_timer *prev; + SLIST_ENTRY(rtadvd_timer) entries; struct rainfo *rai; struct timeval tm; @@ -52,12 +41,9 @@ struct rtadvd_timer { void *update_data; }; -void rtadvd_timer_init(void); struct rtadvd_timer *rtadvd_add_timer(void (*)(void *), void (*)(void *, struct timeval *), void *, void *); void rtadvd_set_timer(struct timeval *, struct rtadvd_timer *); void rtadvd_remove_timer(struct rtadvd_timer **); struct timeval * rtadvd_check_timer(void); struct timeval * rtadvd_timer_rest(struct rtadvd_timer *); -void TIMEVAL_ADD(struct timeval *, struct timeval *, struct timeval *); -void TIMEVAL_SUB(struct timeval *, struct timeval *, struct timeval *); |