1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* The mrouted program is covered by the license in the accompanying file
|
|
|
|
* named "LICENSE". Use of the mrouted program represents acceptance of
|
|
|
|
* the terms and conditions listed in that file.
|
|
|
|
*
|
|
|
|
* The mrouted program is COPYRIGHT 1989 by The Board of Trustees of
|
|
|
|
* Leland Stanford Junior University.
|
1999-01-20 07:55:57 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* callout.c,v 3.8.4.8 1998/01/06 01:58:45 fenner Exp
|
1994-09-08 00:26:13 +00:00
|
|
|
*/
|
|
|
|
|
1998-01-16 07:17:45 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-01-20 07:55:57 +00:00
|
|
|
"$Id: callout.c,v 1.10 1998/01/16 07:17:41 charnier Exp $";
|
1998-01-16 07:17:45 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
/* the code below implements a callout queue */
|
|
|
|
static int id = 0;
|
|
|
|
static struct timeout_q *Q = 0; /* pointer to the beginning of timeout queue */
|
|
|
|
|
|
|
|
struct timeout_q {
|
|
|
|
struct timeout_q *next; /* next event */
|
1995-06-13 18:05:16 +00:00
|
|
|
int id;
|
1995-06-28 17:58:49 +00:00
|
|
|
cfunc_t func; /* function to call */
|
1999-01-20 07:55:57 +00:00
|
|
|
void *data; /* func's data */
|
1994-09-08 00:26:13 +00:00
|
|
|
int time; /* time offset to next event*/
|
|
|
|
};
|
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
#ifdef IGMP_DEBUG
|
|
|
|
static void print_Q __P((void));
|
|
|
|
#else
|
|
|
|
#define print_Q()
|
|
|
|
#endif
|
1995-05-16 00:28:50 +00:00
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
void
|
|
|
|
callout_init()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
Q = (struct timeout_q *) 0;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
void
|
|
|
|
free_all_callouts()
|
|
|
|
{
|
|
|
|
struct timeout_q *p;
|
|
|
|
|
|
|
|
while (Q) {
|
|
|
|
p = Q;
|
|
|
|
Q = Q->next;
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
|
|
|
|
/*
|
1999-01-20 07:55:57 +00:00
|
|
|
* elapsed_time seconds have passed; perform all the events that should
|
|
|
|
* happen.
|
1994-09-08 00:26:13 +00:00
|
|
|
*/
|
1995-06-28 17:58:49 +00:00
|
|
|
void
|
1999-01-20 07:55:57 +00:00
|
|
|
age_callout_queue(elapsed_time)
|
|
|
|
int elapsed_time;
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
struct timeout_q *ptr;
|
1999-01-20 07:55:57 +00:00
|
|
|
int i = 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
for (ptr = Q; ptr; ptr = Q, i++) {
|
|
|
|
if (ptr->time > elapsed_time) {
|
|
|
|
ptr->time -= elapsed_time;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
elapsed_time -= ptr->time;
|
1996-01-06 21:10:30 +00:00
|
|
|
Q = Q->next;
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
log(LOG_DEBUG, 0, "about to call timeout %d (#%d)", ptr->id, i);
|
1995-06-28 17:58:49 +00:00
|
|
|
if (ptr->func)
|
1994-09-08 00:26:13 +00:00
|
|
|
ptr->func(ptr->data);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/*
|
|
|
|
* Return in how many seconds age_callout_queue() would like to be called.
|
|
|
|
* Return -1 if there are no events pending.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
timer_nextTimer()
|
|
|
|
{
|
|
|
|
if (Q) {
|
|
|
|
if (Q->time < 0) {
|
|
|
|
log(LOG_WARNING, 0, "timer_nextTimer top of queue says %d",
|
|
|
|
Q->time);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return Q->time;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1995-06-13 18:05:16 +00:00
|
|
|
/*
|
1994-09-08 00:26:13 +00:00
|
|
|
* sets the timer
|
|
|
|
*/
|
1995-06-28 17:58:49 +00:00
|
|
|
int
|
|
|
|
timer_setTimer(delay, action, data)
|
1994-09-08 00:26:13 +00:00
|
|
|
int delay; /* number of units for timeout */
|
|
|
|
cfunc_t action; /* function to be called on timeout */
|
1999-01-20 07:55:57 +00:00
|
|
|
void *data; /* what to call the timeout function with */
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
struct timeout_q *ptr, *node, *prev;
|
1999-01-20 07:55:57 +00:00
|
|
|
int i = 0;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
|
|
|
/* create a node */
|
1995-06-28 17:58:49 +00:00
|
|
|
node = (struct timeout_q *)malloc(sizeof(struct timeout_q));
|
1995-06-13 18:05:16 +00:00
|
|
|
if (node == 0) {
|
1994-09-08 00:26:13 +00:00
|
|
|
log(LOG_WARNING, 0, "Malloc Failed in timer_settimer\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1995-06-13 18:05:16 +00:00
|
|
|
node->func = action;
|
1994-09-08 00:26:13 +00:00
|
|
|
node->data = data;
|
1995-06-13 18:05:16 +00:00
|
|
|
node->time = delay;
|
|
|
|
node->next = 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
node->id = ++id;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
prev = ptr = Q;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/* insert node in the queue */
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/* if the queue is empty, insert the node and return */
|
|
|
|
if (!Q)
|
|
|
|
Q = node;
|
|
|
|
else {
|
|
|
|
/* chase the pointer looking for the right place */
|
1995-06-28 17:58:49 +00:00
|
|
|
while (ptr) {
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
if (delay < ptr->time) {
|
1994-09-08 00:26:13 +00:00
|
|
|
/* right place */
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
node->next = ptr;
|
|
|
|
if (ptr == Q)
|
|
|
|
Q = node;
|
|
|
|
else
|
|
|
|
prev->next = node;
|
|
|
|
ptr->time -= node->time;
|
|
|
|
print_Q();
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
|
1994-09-08 00:26:13 +00:00
|
|
|
return node->id;
|
1995-06-28 17:58:49 +00:00
|
|
|
} else {
|
1994-09-08 00:26:13 +00:00
|
|
|
/* keep moving */
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
delay -= ptr->time; node->time = delay;
|
|
|
|
prev = ptr;
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
i++;
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
prev->next = node;
|
|
|
|
}
|
|
|
|
print_Q();
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
log(LOG_DEBUG, 0, "created timeout %d (#%d)", node->id, i);
|
1994-09-08 00:26:13 +00:00
|
|
|
return node->id;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
/* returns the time until the timer is scheduled */
|
|
|
|
int
|
|
|
|
timer_leftTimer(timer_id)
|
|
|
|
int timer_id;
|
|
|
|
{
|
|
|
|
struct timeout_q *ptr;
|
|
|
|
int left = 0;
|
|
|
|
|
|
|
|
if (!timer_id)
|
|
|
|
return -1;
|
1994-09-08 00:26:13 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
for (ptr = Q; ptr; ptr = ptr->next) {
|
|
|
|
left += ptr->time;
|
|
|
|
if (ptr->id == timer_id)
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clears the associated timer. Returns 1 if succeeded. */
|
|
|
|
int
|
1995-06-28 17:58:49 +00:00
|
|
|
timer_clearTimer(timer_id)
|
1995-06-13 18:05:16 +00:00
|
|
|
int timer_id;
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
struct timeout_q *ptr, *prev;
|
1999-01-20 07:55:57 +00:00
|
|
|
int i = 0;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
if (!timer_id)
|
1999-01-20 07:55:57 +00:00
|
|
|
return 0;
|
1995-06-28 17:58:49 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
prev = ptr = Q;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* find the right node, delete it. the subsequent node's time
|
|
|
|
* gets bumped up
|
|
|
|
*/
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
print_Q();
|
1995-06-28 17:58:49 +00:00
|
|
|
while (ptr) {
|
|
|
|
if (ptr->id == timer_id) {
|
1994-09-08 00:26:13 +00:00
|
|
|
/* got the right node */
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/* unlink it from the queue */
|
1995-06-28 17:58:49 +00:00
|
|
|
if (ptr == Q)
|
1994-09-08 00:26:13 +00:00
|
|
|
Q = Q->next;
|
|
|
|
else
|
|
|
|
prev->next = ptr->next;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1994-09-08 00:26:13 +00:00
|
|
|
/* increment next node if any */
|
|
|
|
if (ptr->next != 0)
|
|
|
|
(ptr->next)->time += ptr->time;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
if (ptr->data)
|
|
|
|
free(ptr->data);
|
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
log(LOG_DEBUG, 0, "deleted timer %d (#%d)", ptr->id, i);
|
1994-09-08 00:26:13 +00:00
|
|
|
free(ptr);
|
|
|
|
print_Q();
|
1999-01-20 07:55:57 +00:00
|
|
|
return 1;
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
prev = ptr;
|
|
|
|
ptr = ptr->next;
|
1999-01-20 07:55:57 +00:00
|
|
|
i++;
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
log(LOG_DEBUG, 0, "failed to delete timer %d (#%d)", timer_id, i);
|
1994-09-08 00:26:13 +00:00
|
|
|
print_Q();
|
1999-01-20 07:55:57 +00:00
|
|
|
return 0;
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
|
|
|
|
1995-06-28 17:58:49 +00:00
|
|
|
#ifdef IGMP_DEBUG
|
1994-09-08 00:26:13 +00:00
|
|
|
/*
|
|
|
|
* debugging utility
|
|
|
|
*/
|
1995-06-28 17:58:49 +00:00
|
|
|
static void
|
|
|
|
print_Q()
|
1994-09-08 00:26:13 +00:00
|
|
|
{
|
|
|
|
struct timeout_q *ptr;
|
1995-06-13 18:05:16 +00:00
|
|
|
|
1999-01-20 07:55:57 +00:00
|
|
|
IF_DEBUG(DEBUG_TIMEOUT)
|
|
|
|
for (ptr = Q; ptr; ptr = ptr->next)
|
|
|
|
log(LOG_DEBUG, 0, "(%d,%d) ", ptr->id, ptr->time);
|
1994-09-08 00:26:13 +00:00
|
|
|
}
|
1995-06-28 17:58:49 +00:00
|
|
|
#endif /* IGMP_DEBUG */
|