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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef OTIME_H
#define OTIME_H
#include "common.h"
#include "integer.h"
#include "buffer.h"
#include "thread.h"
struct frequency_limit
{
int max;
int per;
int n;
time_t reset;
};
struct frequency_limit *frequency_limit_init (int max, int per);
void frequency_limit_free (struct frequency_limit *f);
bool frequency_limit_event_allowed (struct frequency_limit *f);
#ifdef WIN32
int gettimeofday(struct timeval *tv, void *tz);
#endif
/* format a time_t as ascii, or use current time if 0 */
const char* time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc);
/* struct timeval functions */
const char *tv_string (const struct timeval *tv, struct gc_arena *gc);
const char *tv_string_abs (const struct timeval *tv, struct gc_arena *gc);
extern time_t now; /* updated frequently to time(NULL) */
void time_test (void);
#if TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)
void update_now (const time_t system_time);
extern time_t now_usec;
void update_now_usec (struct timeval *tv);
static inline int
openvpn_gettimeofday (struct timeval *tv, void *tz)
{
const int status = gettimeofday (tv, tz);
if (!status)
{
update_now_usec (tv);
tv->tv_sec = now;
tv->tv_usec = now_usec;
}
return status;
}
static inline void
update_time (void)
{
#ifdef WIN32
/* on WIN32, gettimeofday is faster than time(NULL) */
struct timeval tv;
openvpn_gettimeofday (&tv, NULL);
#else
update_now (time (NULL));
#endif
}
#else /* !(TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)) */
static inline void
update_time (void)
{
#if defined(WIN32) && defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
if (!gettimeofday (&tv, NULL))
{
if (tv.tv_sec != now)
now = tv.tv_sec;
}
#else
const time_t real_time = time (NULL);
if (real_time != now)
now = real_time;
#endif
}
#ifdef HAVE_GETTIMEOFDAY
static inline int
openvpn_gettimeofday (struct timeval *tv, void *tz)
{
return gettimeofday (tv, tz);
}
#endif
#endif /* TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY) */
static inline time_t
openvpn_time (time_t *t)
{
update_time ();
if (t)
*t = now;
return now;
}
static inline void
tv_clear (struct timeval *tv)
{
tv->tv_sec = 0;
tv->tv_usec = 0;
}
static inline bool
tv_defined (const struct timeval *tv)
{
return tv->tv_sec > 0 && tv->tv_usec > 0;
}
/* return tv1 - tv2 in usec, constrained by max_seconds */
static inline int
tv_subtract (const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
{
const int max_usec = max_seconds * 1000000;
const int sec_diff = tv1->tv_sec - tv2->tv_sec;
if (sec_diff > ((int)max_seconds + 10))
return max_usec;
else if (sec_diff < -((int)max_seconds + 10))
return -max_usec;
return constrain_int (sec_diff * 1000000 + (tv1->tv_usec - tv2->tv_usec), -max_usec, max_usec);
}
static inline void
tv_add (struct timeval *dest, const struct timeval *src)
{
dest->tv_sec += src->tv_sec;
dest->tv_usec += src->tv_usec;
dest->tv_sec += (dest->tv_usec >> 20);
dest->tv_usec &= 0x000FFFFF;
if (dest->tv_usec >= 1000000)
{
dest->tv_usec -= 1000000;
dest->tv_sec += 1;
}
}
static inline bool
tv_lt (const struct timeval *t1, const struct timeval *t2)
{
if (t1->tv_sec < t2->tv_sec)
return true;
else if (t1->tv_sec > t2->tv_sec)
return false;
else
return t1->tv_usec < t2->tv_usec;
}
static inline bool
tv_le (const struct timeval *t1, const struct timeval *t2)
{
if (t1->tv_sec < t2->tv_sec)
return true;
else if (t1->tv_sec > t2->tv_sec)
return false;
else
return t1->tv_usec <= t2->tv_usec;
}
static inline bool
tv_ge (const struct timeval *t1, const struct timeval *t2)
{
if (t1->tv_sec > t2->tv_sec)
return true;
else if (t1->tv_sec < t2->tv_sec)
return false;
else
return t1->tv_usec >= t2->tv_usec;
}
static inline bool
tv_gt (const struct timeval *t1, const struct timeval *t2)
{
if (t1->tv_sec > t2->tv_sec)
return true;
else if (t1->tv_sec < t2->tv_sec)
return false;
else
return t1->tv_usec > t2->tv_usec;
}
static inline bool
tv_eq (const struct timeval *t1, const struct timeval *t2)
{
return t1->tv_sec == t2->tv_sec && t1->tv_usec == t2->tv_usec;
}
static inline void
tv_delta (struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
{
int sec = t2->tv_sec - t1->tv_sec;
int usec = t2->tv_usec - t1->tv_usec;
while (usec < 0)
{
usec += 1000000;
sec -= 1;
}
if (sec < 0)
usec = sec = 0;
dest->tv_sec = sec;
dest->tv_usec = usec;
}
#define TV_WITHIN_SIGMA_MAX_SEC 600
#define TV_WITHIN_SIGMA_MAX_USEC (TV_WITHIN_SIGMA_MAX_SEC * 1000000)
/*
* Is t1 and t2 within sigma microseconds of each other?
*/
static inline bool
tv_within_sigma (const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
{
const int delta = tv_subtract (t1, t2, TV_WITHIN_SIGMA_MAX_SEC); /* sigma should be less than 10 minutes */
return -(int)sigma <= delta && delta <= (int)sigma;
}
/*
* Used to determine in how many seconds we should be
* called again.
*/
static inline void
interval_earliest_wakeup (interval_t *wakeup, time_t at, time_t current) {
if (at > current)
{
const interval_t delta = (interval_t) (at - current);
if (delta < *wakeup)
*wakeup = delta;
if (*wakeup < 0)
*wakeup = 0;
}
}
#endif
|