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
8
* Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
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.
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.
19
* You should have received a copy of the GNU General Public License
20
* along with this program (see the file COPYING included with this
21
* distribution); if not, write to the Free Software Foundation, Inc.,
22
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#elif defined(_MSC_VER)
28
#include "config-msvc.h"
37
time_t now = 0; /* GLOBAL */
39
#if TIME_BACKTRACK_PROTECTION
41
static time_t now_adj = 0; /* GLOBAL */
42
time_t now_usec = 0; /* GLOBAL */
45
* Try to filter out time instability caused by the system
46
* clock backtracking or jumping forward.
50
update_now (const time_t system_time)
52
const int forward_threshold = 86400; /* threshold at which to dampen forward jumps */
53
const int backward_trigger = 10; /* backward jump must be >= this many seconds before we adjust */
54
time_t real_time = system_time + now_adj;
58
const time_t overshoot = real_time - now - 1;
59
if (overshoot > forward_threshold && now_adj >= overshoot)
62
real_time -= overshoot;
66
else if (real_time < now - backward_trigger)
67
now_adj += (now - real_time);
71
update_now_usec (struct timeval *tv)
73
const time_t last = now;
74
update_now (tv->tv_sec);
75
if (now > last || (now == last && tv->tv_usec > now_usec))
76
now_usec = tv->tv_usec;
79
#endif /* TIME_BACKTRACK_PROTECTION */
82
* Return a numerical string describing a struct timeval.
85
tv_string (const struct timeval *tv, struct gc_arena *gc)
87
struct buffer out = alloc_buf_gc (64, gc);
88
buf_printf (&out, "[%d/%d]",
95
* Return an ascii string describing an absolute
96
* date/time in a struct timeval.
100
tv_string_abs (const struct timeval *tv, struct gc_arena *gc)
102
return time_string ((time_t) tv->tv_sec,
108
/* format a time_t as ascii, or use current time if 0 */
111
time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc)
113
struct buffer out = alloc_buf_gc (64, gc);
123
gettimeofday (&tv, NULL);
127
buf_printf (&out, "%s", ctime(&t));
128
buf_rmtail (&out, '\n');
130
if (show_usec && tv.tv_usec)
131
buf_printf (&out, " us=%d", (int)tv.tv_usec);
137
* Limit the frequency of an event stream.
139
* Used to control maximum rate of new
140
* incoming connections.
143
struct frequency_limit *
144
frequency_limit_init (int max, int per)
146
struct frequency_limit *f;
148
ASSERT (max >= 0 && per >= 0);
150
ALLOC_OBJ (f, struct frequency_limit);
159
frequency_limit_free (struct frequency_limit *f)
165
frequency_limit_event_allowed (struct frequency_limit *f)
170
if (now >= f->reset + f->per)
175
ret = (++f->n <= f->max);
189
for (i = 0; i < 10000; ++i)
192
gettimeofday (&tv, NULL);
194
msg (M_INFO, "t=%u s=%u us=%u",
196
(unsigned int)tv.tv_sec,
197
(unsigned int)tv.tv_usec);