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-2018 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, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef OTIME_H
25 #define OTIME_H
26 
27 #include "common.h"
28 #include "integer.h"
29 #include "buffer.h"
30 
32 {
33  int max;
34  int per;
35  int n;
36  time_t reset;
37 };
38 
40 
42 
44 
45 /* format a time_t as ascii, or use current time if 0 */
46 const char *time_string(time_t t, int usec, bool show_usec, struct gc_arena *gc);
47 
48 /* struct timeval functions */
49 
50 const char *tv_string(const struct timeval *tv, struct gc_arena *gc);
51 
52 const char *tv_string_abs(const struct timeval *tv, struct gc_arena *gc);
53 
54 extern time_t now; /* updated frequently to time(NULL) */
55 
56 void time_test(void);
57 
58 #if TIME_BACKTRACK_PROTECTION
59 
60 void update_now(const time_t system_time);
61 
62 extern time_t now_usec;
63 void update_now_usec(struct timeval *tv);
64 
65 static inline int
66 openvpn_gettimeofday(struct timeval *tv, void *tz)
67 {
68  const int status = gettimeofday(tv, tz);
69  if (!status)
70  {
71  update_now_usec(tv);
72  tv->tv_sec = now;
73  tv->tv_usec = now_usec;
74  }
75  return status;
76 }
77 
78 static inline void
79 update_time(void)
80 {
81 #ifdef _WIN32
82  /* on _WIN32, gettimeofday is faster than time(NULL) */
83  struct timeval tv;
84  openvpn_gettimeofday(&tv, NULL);
85 #else
86  update_now(time(NULL));
87 #endif
88 }
89 
90 #else /* !TIME_BACKTRACK_PROTECTION */
91 
92 static inline void
94 {
95 #if defined(_WIN32)
96  /* on _WIN32, gettimeofday is faster than time(NULL) */
97  struct timeval tv;
98  if (!gettimeofday(&tv, NULL))
99  {
100  if (tv.tv_sec != now)
101  {
102  now = tv.tv_sec;
103  }
104  }
105 #else /* if defined(_WIN32) */
106  const time_t real_time = time(NULL);
107  if (real_time != now)
108  {
109  now = real_time;
110  }
111 #endif
112 }
113 
114 static inline int
115 openvpn_gettimeofday(struct timeval *tv, void *tz)
116 {
117  return gettimeofday(tv, tz);
118 }
119 
120 #endif /* TIME_BACKTRACK_PROTECTION */
121 
122 static inline time_t
123 openvpn_time(time_t *t)
124 {
125  update_time();
126  if (t)
127  {
128  *t = now;
129  }
130  return now;
131 }
132 
133 static inline void
134 tv_clear(struct timeval *tv)
135 {
136  tv->tv_sec = 0;
137  tv->tv_usec = 0;
138 }
139 
140 static inline bool
141 tv_defined(const struct timeval *tv)
142 {
143  return tv->tv_sec > 0 && tv->tv_usec > 0;
144 }
145 
146 /* return tv1 - tv2 in usec, constrained by max_seconds */
147 static inline int
148 tv_subtract(const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
149 {
150  const int max_usec = max_seconds * 1000000;
151  const int sec_diff = tv1->tv_sec - tv2->tv_sec;
152 
153  if (sec_diff > ((int)max_seconds + 10))
154  {
155  return max_usec;
156  }
157  else if (sec_diff < -((int)max_seconds + 10))
158  {
159  return -max_usec;
160  }
161  return constrain_int(sec_diff * 1000000 + (tv1->tv_usec - tv2->tv_usec), -max_usec, max_usec);
162 }
163 
164 static inline void
165 tv_add(struct timeval *dest, const struct timeval *src)
166 {
167  dest->tv_sec += src->tv_sec;
168  dest->tv_usec += src->tv_usec;
169  dest->tv_sec += (dest->tv_usec >> 20);
170  dest->tv_usec &= 0x000FFFFF;
171  if (dest->tv_usec >= 1000000)
172  {
173  dest->tv_usec -= 1000000;
174  dest->tv_sec += 1;
175  }
176 }
177 
178 static inline bool
179 tv_lt(const struct timeval *t1, const struct timeval *t2)
180 {
181  if (t1->tv_sec < t2->tv_sec)
182  {
183  return true;
184  }
185  else if (t1->tv_sec > t2->tv_sec)
186  {
187  return false;
188  }
189  else
190  {
191  return t1->tv_usec < t2->tv_usec;
192  }
193 }
194 
195 static inline bool
196 tv_le(const struct timeval *t1, const struct timeval *t2)
197 {
198  if (t1->tv_sec < t2->tv_sec)
199  {
200  return true;
201  }
202  else if (t1->tv_sec > t2->tv_sec)
203  {
204  return false;
205  }
206  else
207  {
208  return t1->tv_usec <= t2->tv_usec;
209  }
210 }
211 
212 static inline bool
213 tv_ge(const struct timeval *t1, const struct timeval *t2)
214 {
215  if (t1->tv_sec > t2->tv_sec)
216  {
217  return true;
218  }
219  else if (t1->tv_sec < t2->tv_sec)
220  {
221  return false;
222  }
223  else
224  {
225  return t1->tv_usec >= t2->tv_usec;
226  }
227 }
228 
229 static inline bool
230 tv_gt(const struct timeval *t1, const struct timeval *t2)
231 {
232  if (t1->tv_sec > t2->tv_sec)
233  {
234  return true;
235  }
236  else if (t1->tv_sec < t2->tv_sec)
237  {
238  return false;
239  }
240  else
241  {
242  return t1->tv_usec > t2->tv_usec;
243  }
244 }
245 
246 static inline bool
247 tv_eq(const struct timeval *t1, const struct timeval *t2)
248 {
249  return t1->tv_sec == t2->tv_sec && t1->tv_usec == t2->tv_usec;
250 }
251 
252 static inline void
253 tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
254 {
255  int sec = t2->tv_sec - t1->tv_sec;
256  int usec = t2->tv_usec - t1->tv_usec;
257 
258  while (usec < 0)
259  {
260  usec += 1000000;
261  sec -= 1;
262  }
263 
264  if (sec < 0)
265  {
266  usec = sec = 0;
267  }
268 
269  dest->tv_sec = sec;
270  dest->tv_usec = usec;
271 }
272 
273 #define TV_WITHIN_SIGMA_MAX_SEC 600
274 #define TV_WITHIN_SIGMA_MAX_USEC (TV_WITHIN_SIGMA_MAX_SEC * 1000000)
275 
276 /*
277  * Is t1 and t2 within sigma microseconds of each other?
278  */
279 static inline bool
280 tv_within_sigma(const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
281 {
282  const int delta = tv_subtract(t1, t2, TV_WITHIN_SIGMA_MAX_SEC); /* sigma should be less than 10 minutes */
283  return -(int)sigma <= delta && delta <= (int)sigma;
284 }
285 
286 /*
287  * Used to determine in how many seconds we should be
288  * called again.
289  */
290 static inline void
291 interval_earliest_wakeup(interval_t *wakeup, time_t at, time_t current)
292 {
293  if (at > current)
294  {
295  const interval_t delta = (interval_t) (at - current);
296  if (delta < *wakeup)
297  {
298  *wakeup = delta;
299  }
300  if (*wakeup < 0)
301  {
302  *wakeup = 0;
303  }
304  }
305 }
306 
307 #endif /* ifndef OTIME_H */
static void tv_delta(struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:253
static int tv_subtract(const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
Definition: otime.h:148
static void interval_earliest_wakeup(interval_t *wakeup, time_t at, time_t current)
Definition: otime.h:291
static void tv_clear(struct timeval *tv)
Definition: otime.h:134
time_t reset
Definition: otime.h:36
bool frequency_limit_event_allowed(struct frequency_limit *f)
Definition: otime.c:170
const char * tv_string_abs(const struct timeval *tv, struct gc_arena *gc)
Definition: otime.c:103
static bool tv_defined(const struct timeval *tv)
Definition: otime.h:141
string f
Definition: http-client.py:6
time_t now_usec
Definition: otime.c:41
static bool tv_ge(const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:213
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition: otime.h:165
static SERVICE_STATUS status
Definition: automatic.c:43
void update_now_usec(struct timeval *tv)
Definition: otime.c:72
static time_t openvpn_time(time_t *t)
Definition: otime.h:123
const char * tv_string(const struct timeval *tv, struct gc_arena *gc)
Definition: otime.c:88
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition: otime.h:115
time_t now
Definition: otime.c:36
static bool tv_le(const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:196
void time_test(void)
void frequency_limit_free(struct frequency_limit *f)
Definition: otime.c:164
const char * time_string(time_t t, int usec, bool show_usec, struct gc_arena *gc)
Definition: otime.c:114
void update_now(const time_t system_time)
Definition: otime.c:49
static int constrain_int(int x, int min, int max)
Definition: integer.h:70
static bool tv_lt(const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:179
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
int interval_t
Definition: common.h:45
static bool tv_gt(const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:230
static void update_time(void)
Definition: otime.h:93
char * dest
Definition: compat-lz4.h:431
static bool tv_within_sigma(const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
Definition: otime.h:280
#define TV_WITHIN_SIGMA_MAX_SEC
Definition: otime.h:273
struct frequency_limit * frequency_limit_init(int max, int per)
Definition: otime.c:149
static bool tv_eq(const struct timeval *t1, const struct timeval *t2)
Definition: otime.h:247