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
41#if defined(__GNUC__) || defined(__clang__)
42#pragma GCC diagnostic push
43#pragma GCC diagnostic ignored "-Wconversion"
44#endif
45
46bool
47event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry)
48{
49 if (!et->defined)
50 {
51 return false;
52 }
53
54 bool ret = false;
55 time_t wakeup = event_timeout_remaining(et);
56
57 if (wakeup <= 0)
58 {
59#if INTERVAL_DEBUG
60 dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, et_const_retry);
61#endif
62 if (et_const_retry < 0)
63 {
64 et->last = now;
65 wakeup = et->n;
66 ret = true;
67 }
68 else
69 {
70 wakeup = et_const_retry;
71 }
72 }
73
74 if (tv && wakeup < tv->tv_sec)
75 {
76#if INTERVAL_DEBUG
77 dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", (int)wakeup, et->n,
78 et_const_retry);
79#endif
80 tv->tv_sec = wakeup;
81 tv->tv_usec = 0;
82 }
83 return ret;
84}
85
86#if defined(__GNUC__) || defined(__clang__)
87#pragma GCC diagnostic pop
88#endif
#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:47
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:172
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