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-2005 OpenVPN Solutions LLC <info@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
26 #include "config-win32.h"
37 volatile time_t now
; /* GLOBAL */
40 * Return a numerical string describing a struct timeval.
43 tv_string (const struct timeval
*tv
, struct gc_arena
*gc
)
45 struct buffer out
= alloc_buf_gc (64, gc
);
46 buf_printf (&out
, "[%d/%d]",
53 * Return an ascii string describing an absolute
54 * date/time in a struct timeval.
58 tv_string_abs (const struct timeval
*tv
, struct gc_arena
*gc
)
60 return time_string ((time_t) tv
->tv_sec
,
66 /* format a time_t as ascii, or use current time if 0 */
69 time_string (time_t t
, int usec
, bool show_usec
, struct gc_arena
*gc
)
71 struct buffer out
= alloc_buf_gc (64, gc
);
81 #ifdef HAVE_GETTIMEOFDAY
82 if (gettimeofday (&tv
, NULL
))
85 tv
.tv_sec
= time (NULL
);
90 mutex_lock_static (L_CTIME
);
91 buf_printf (&out
, "%s", ctime ((const time_t *)&tv
.tv_sec
));
92 mutex_unlock_static (L_CTIME
);
93 buf_rmtail (&out
, '\n');
95 if (show_usec
&& tv
.tv_usec
)
96 buf_printf (&out
, " us=%d", (int)tv
.tv_usec
);
102 * Limit the frequency of an event stream.
104 * Used to control maximum rate of new
105 * incoming connections.
108 struct frequency_limit
*
109 frequency_limit_init (int max
, int per
)
111 struct frequency_limit
*f
;
113 ASSERT (max
>= 0 && per
>= 0);
115 ALLOC_OBJ (f
, struct frequency_limit
);
124 frequency_limit_free (struct frequency_limit
*f
)
130 frequency_limit_event_allowed (struct frequency_limit
*f
)
135 if (now
>= f
->reset
+ f
->per
)
140 ret
= (++f
->n
<= f
->max
);
149 static double counterPerMicrosecond
= -1.0; /* GLOBAL */
150 static unsigned __int64 frequency
= 0; /* GLOBAL */
151 static unsigned __int64 timeSecOffset
= 0; /* GLOBAL */
152 static unsigned __int64 startPerformanceCounter
= 0; /* GLOBAL */
155 * gettimeofday for windows
157 * CounterPerMicrosecond is the number of counts per microsecond.
158 * Double is required if we have less than 1 counter per microsecond. This has not been tested.
159 * On a PIII 700, I get about 3.579545. This is guaranteed not to change while the processor is running.
160 * We really don't need to check for loop detection. On my machine it would take about 59645564 days to loop.
161 * (2^64) / frequency / 60 / 60 / 24.
165 gettimeofday(struct timeval
*tv
, void *tz
)
167 unsigned __int64 counter
;
169 QueryPerformanceCounter((LARGE_INTEGER
*) &counter
);
171 if (counter
< startPerformanceCounter
|| counterPerMicrosecond
== -1.0)
174 mutex_lock (L_GETTIMEOFDAY
);
176 QueryPerformanceFrequency((LARGE_INTEGER
*) &frequency
);
178 counterPerMicrosecond
= (double) ((__int64
) frequency
) / 1000000.0f
;
181 QueryPerformanceCounter((LARGE_INTEGER
*) &counter
);
182 startPerformanceCounter
= counter
;
184 counter
/= frequency
;
186 timeSecOffset
= t
- counter
;
188 mutex_unlock (L_GETTIMEOFDAY
);
189 QueryPerformanceCounter((LARGE_INTEGER
*) &counter
);
192 tv
->tv_sec
= (counter
/ frequency
) + timeSecOffset
;
193 tv
->tv_usec
= ((__int64
) (((__int64
) counter
) / counterPerMicrosecond
) % 1000000);