1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/* $OpenBSD: timer.c,v 1.11 2008/05/08 09:53:12 henning Exp $ */
/*
* Copyright (c) 2003-2007 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <stdlib.h>
#include "bgpd.h"
#include "session.h"
struct peer_timer *
timer_get(struct peer *p, enum Timer timer)
{
struct peer_timer *pt;
TAILQ_FOREACH(pt, &p->timers, entry)
if (pt->type == timer)
break;
return (pt);
}
struct peer_timer *
timer_nextisdue(struct peer *p)
{
struct peer_timer *pt;
pt = TAILQ_FIRST(&p->timers);
if (pt != NULL && pt->val > 0 && pt->val <= time(NULL))
return (pt);
return (NULL);
}
time_t
timer_nextduein(struct peer *p)
{
struct peer_timer *pt;
if ((pt = TAILQ_FIRST(&p->timers)) != NULL && pt->val > 0)
return (pt->val);
return (-1);
}
int
timer_running(struct peer *p, enum Timer timer, time_t *left)
{
struct peer_timer *pt = timer_get(p, timer);
if (pt != NULL && pt->val > 0) {
if (left != NULL)
*left = pt->val - time(NULL);
return (1);
}
return (0);
}
void
timer_set(struct peer *p, enum Timer timer, u_int offset)
{
struct peer_timer *t, *pt = timer_get(p, timer);
if (pt == NULL) { /* have to create */
if ((pt = malloc(sizeof(*pt))) == NULL)
fatal("timer_set");
pt->type = timer;
} else {
if (pt->val == time(NULL) + (time_t)offset)
return;
TAILQ_REMOVE(&p->timers, pt, entry);
}
pt->val = time(NULL) + offset;
TAILQ_FOREACH(t, &p->timers, entry)
if (t->val == 0 || t->val > pt->val)
break;
if (t != NULL)
TAILQ_INSERT_BEFORE(t, pt, entry);
else
TAILQ_INSERT_TAIL(&p->timers, pt, entry);
}
void
timer_stop(struct peer *p, enum Timer timer)
{
struct peer_timer *pt = timer_get(p, timer);
if (pt != NULL) {
pt->val = 0;
TAILQ_REMOVE(&p->timers, pt, entry);
TAILQ_INSERT_TAIL(&p->timers, pt, entry);
}
}
void
timer_remove(struct peer *p, enum Timer timer)
{
struct peer_timer *pt = timer_get(p, timer);
if (pt != NULL) {
TAILQ_REMOVE(&p->timers, pt, entry);
free(pt);
}
}
void
timer_remove_all(struct peer *p)
{
struct peer_timer *pt;
while ((pt = TAILQ_FIRST(&p->timers)) != NULL) {
TAILQ_REMOVE(&p->timers, pt, entry);
free(pt);
}
}
|