summaryrefslogtreecommitdiff
path: root/usr.sbin/bgpd/timer.c
blob: a924a5d43394e6ca74ba67c489c7c6527e2af167 (plain)
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
/*	$OpenBSD: timer.c,v 1.4 2007/12/23 18:56:17 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 "bgpd.h"
#include "session.h"

time_t *
timer_get(struct peer *p, enum Timer timer)
{
	switch (timer) {
	case Timer_None:
		fatal("timer_get called with Timer_None");
	case Timer_ConnectRetry:
		return (&p->ConnectRetryTimer);
	case Timer_Keepalive:
		return (&p->KeepaliveTimer);
	case Timer_Hold:
		return (&p->HoldTimer);
	case Timer_IdleHold:
		return (&p->IdleHoldTimer);
	case Timer_IdleHoldReset:
		return (&p->IdleHoldResetTimer);
	case Timer_Max:
		fatal("timer_get called with Timer_Max");
	}

	fatal("King Bula lost in time");
}

int
timer_due(struct peer *p, enum Timer timer)
{
	time_t	*t = timer_get(p, timer);

	if (t != NULL && *t > 0 && *t <= time(NULL))
		return (1);
	return (0);
}

time_t
timer_nextduein(struct peer *p)
{
	u_int	i;
	time_t	d, r = -1;

	for (i = 1; i < Timer_Max; i++)
		if (timer_running(p, i, &d))
			if (r == -1 || d < r)
				r = d;

	return (r);
}

int
timer_running(struct peer *p, enum Timer timer, time_t *left)
{
	time_t	*t = timer_get(p, timer);

	if (t != NULL && *t > 0) {
		if (left != NULL)
			*left = *t - time(NULL);
		return (1);
	}
	return (0);
}

void
timer_set(struct peer *p, enum Timer timer, u_int offset)
{
	time_t	*t = timer_get(p, timer);

	*t = time(NULL) + offset;
}

void
timer_stop(struct peer *p, enum Timer timer)
{
	time_t	*t = timer_get(p, timer);

	if (t != NULL)
		*t = 0;
}