OpenVPN
interval.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "interval.h"
30
31#include "memdbg.h"
32
33void
34interval_init(struct interval *top, int horizon, int refresh)
35{
36 CLEAR(*top);
37 top->refresh = refresh;
38 top->horizon = horizon;
39}
40
41bool
42event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
43{
44 if (!et->defined)
45 {
46 return false;
47 }
48
49 bool ret = false;
50 time_t wakeup = event_timeout_remaining(et);
51
52 if (wakeup <= 0)
53 {
54#if INTERVAL_DEBUG
55 dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, et_const_retry);
56#endif
57 if (et_const_retry < 0)
58 {
59 et->last = now;
60 wakeup = et->n;
61 ret = true;
62 }
63 else
64 {
65 wakeup = et_const_retry;
66 }
67 }
68
69 if (tv && wakeup < tv->tv_sec)
70 {
71#if INTERVAL_DEBUG
72 dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", (int)wakeup, et->n,
73 et_const_retry);
74#endif
75 tv->tv_sec = wakeup;
76 tv->tv_usec = 0;
77 }
78 return ret;
79}
#define D_INTERVAL
Definition errlevel.h:157
bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
This is the principal function for testing and triggering recurring timers.
Definition interval.c:42
void interval_init(struct interval *top, int horizon, int refresh)
Definition interval.c:34
static interval_t event_timeout_remaining(struct event_timeout *et)
Returns the time until the timeout should triggered, from now.
Definition interval.h:217
#define CLEAR(x)
Definition basic.h:32
#define dmsg(flags,...)
Definition error.h:170
time_t now
Definition otime.c:33
interval_t n
periodic interval for periodic timeouts
Definition interval.h:137
bool defined
This timeout is active.
Definition interval.h:136
time_t last
time of last event
Definition interval.h:138
interval_t horizon
Definition interval.h:44
interval_t refresh
Definition interval.h:43