OpenVPN
otime.h
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-2026 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#ifndef OTIME_H
24#define OTIME_H
25
26#include "common.h"
27#include "integer.h"
28#include "buffer.h"
29
30#ifdef _WIN32
31typedef long tv_sec_t;
32typedef long tv_usec_t;
33#else
34typedef time_t tv_sec_t;
35typedef suseconds_t tv_usec_t;
36#endif
37
39{
40 int max;
41 int per;
42 int n;
43 time_t reset;
44};
45
47
49
51
52/* format a time_t as ascii, or use current time if 0 */
53const char *time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc);
54
55/* struct timeval functions */
56
57const char *tv_string(const struct timeval *tv, struct gc_arena *gc);
58
59const char *tv_string_abs(const struct timeval *tv, struct gc_arena *gc);
60
61extern time_t now; /* updated frequently to time(NULL) */
62
63void time_test(void);
64
65void update_now(const time_t system_time);
66
67extern time_t now_usec;
68void update_now_usec(struct timeval *tv);
69
70static inline int
71openvpn_gettimeofday(struct timeval *tv, void *tz)
72{
73 const int status = gettimeofday(tv, tz);
74 if (!status)
75 {
77 tv->tv_sec = (tv_sec_t)now;
78 tv->tv_usec = (tv_usec_t)now_usec;
79 }
80 return status;
81}
82
83static inline void
85{
86#ifdef _WIN32
87 /* on _WIN32, gettimeofday is faster than time(NULL) */
88 struct timeval tv;
89 openvpn_gettimeofday(&tv, NULL);
90#else
91 update_now(time(NULL));
92 now_usec = 0;
93#endif
94}
95
96static inline void
97tv_clear(struct timeval *tv)
98{
99 tv->tv_sec = 0;
100 tv->tv_usec = 0;
101}
102
103static inline bool
104tv_defined(const struct timeval *tv)
105{
106 return tv->tv_sec > 0 && tv->tv_usec > 0;
107}
108
109/* return tv1 - tv2 in usec, constrained by max_seconds */
110static inline int
111tv_subtract(const struct timeval *tv1, const struct timeval *tv2, const int max_seconds)
112{
113 const int max_usec = max_seconds * 1000000;
114 const tv_sec_t sec_diff = tv1->tv_sec - tv2->tv_sec;
115
116 if (sec_diff > (max_seconds + 10))
117 {
118 return max_usec;
119 }
120 else if (sec_diff < -(max_seconds + 10))
121 {
122 return -max_usec;
123 }
124 const time_t complete_diff = sec_diff * 1000000 + (tv1->tv_usec - tv2->tv_usec);
125 return constrain_int((int)complete_diff, -max_usec, max_usec);
126}
127
128static inline void
129tv_add(struct timeval *dest, const struct timeval *src)
130{
131 dest->tv_sec += src->tv_sec;
132 dest->tv_usec += src->tv_usec;
133 dest->tv_sec += (dest->tv_usec >> 20);
134 dest->tv_usec &= 0x000FFFFF;
135 if (dest->tv_usec >= 1000000)
136 {
137 dest->tv_usec -= 1000000;
138 dest->tv_sec += 1;
139 }
140}
141
142static inline void
143tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
144{
145 tv_sec_t sec = t2->tv_sec - t1->tv_sec;
146 tv_usec_t usec = t2->tv_usec - t1->tv_usec;
147
148 while (usec < 0)
149 {
150 usec += 1000000;
151 sec -= 1;
152 }
153
154 if (sec < 0)
155 {
156 usec = sec = 0;
157 }
158
159 dest->tv_sec = sec;
160 dest->tv_usec = usec;
161}
162
163#define TV_WITHIN_SIGMA_MAX_SEC 600
164#define TV_WITHIN_SIGMA_MAX_USEC (TV_WITHIN_SIGMA_MAX_SEC * 1000000)
165
166/*
167 * Is t1 and t2 within sigma microseconds of each other?
168 */
169static inline bool
170tv_within_sigma(const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
171{
172 /* sigma should be less than 10 minutes */
173 const int delta = tv_subtract(t1, t2, TV_WITHIN_SIGMA_MAX_SEC);
174 return -(int)sigma <= delta && delta <= (int)sigma;
175}
176
177/*
178 * Used to determine in how many seconds we should be
179 * called again.
180 */
181static inline void
182interval_earliest_wakeup(interval_t *wakeup, time_t at, time_t current)
183{
184 if (at > current)
185 {
186 const interval_t delta = (interval_t)(at - current);
187 if (delta < *wakeup)
188 {
189 *wakeup = delta;
190 }
191 if (*wakeup < 0)
192 {
193 *wakeup = 0;
194 }
195 }
196}
197
198#endif /* ifndef OTIME_H */
Buffer management functions and garbage collection.
int interval_t
Definition common.h:37
static int constrain_int(int x, int min, int max)
Definition integer.h:118
static SERVICE_STATUS status
Definition interactive.c:52
static void tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
Definition otime.h:143
static void interval_earliest_wakeup(interval_t *wakeup, time_t at, time_t current)
Definition otime.h:182
const char * time_string(time_t t, tv_usec_t usec, bool show_usec, struct gc_arena *gc)
Definition otime.c:104
time_t now_usec
Definition otime.c:36
struct frequency_limit * frequency_limit_init(int max, int per)
Definition otime.c:137
void update_now(const time_t system_time)
Definition otime.c:44
void update_now_usec(struct timeval *tv)
Definition otime.c:69
time_t now
Definition otime.c:33
void frequency_limit_free(struct frequency_limit *f)
Definition otime.c:152
const char * tv_string(const struct timeval *tv, struct gc_arena *gc)
Definition otime.c:83
const char * tv_string_abs(const struct timeval *tv, struct gc_arena *gc)
Definition otime.c:96
bool frequency_limit_event_allowed(struct frequency_limit *f)
Definition otime.c:158
static void update_time(void)
Definition otime.h:84
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition otime.h:71
long tv_sec_t
Definition otime.h:31
void time_test(void)
static void tv_clear(struct timeval *tv)
Definition otime.h:97
static bool tv_defined(const struct timeval *tv)
Definition otime.h:104
#define TV_WITHIN_SIGMA_MAX_SEC
Definition otime.h:163
static bool tv_within_sigma(const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
Definition otime.h:170
static int tv_subtract(const struct timeval *tv1, const struct timeval *tv2, const int max_seconds)
Definition otime.h:111
long tv_usec_t
Definition otime.h:32
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition otime.h:129
time_t reset
Definition otime.h:43
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct gc_arena gc
Definition test_ssl.c:122