svn cleanup
[anytun.git] / openvpn / otime.c
blobad9cbbfea2085e45a98da766593efe7c29c3fb29
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.
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
25 #ifdef WIN32
26 #include "config-win32.h"
27 #else
28 #include "config.h"
29 #endif
31 #include "syshead.h"
33 #include "otime.h"
35 #include "memdbg.h"
37 volatile time_t now; /* GLOBAL */
39 /*
40 * Return a numerical string describing a struct timeval.
42 const char *
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]",
47 (int) tv->tv_sec,
48 (int )tv->tv_usec);
49 return BSTR (&out);
52 /*
53 * Return an ascii string describing an absolute
54 * date/time in a struct timeval.
57 const char *
58 tv_string_abs (const struct timeval *tv, struct gc_arena *gc)
60 return time_string ((time_t) tv->tv_sec,
61 (int) tv->tv_usec,
62 true,
63 gc);
66 /* format a time_t as ascii, or use current time if 0 */
68 const char *
69 time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc)
71 struct buffer out = alloc_buf_gc (64, gc);
72 struct timeval tv;
74 if (t)
76 tv.tv_sec = t;
77 tv.tv_usec = usec;
79 else
81 #ifdef HAVE_GETTIMEOFDAY
82 if (gettimeofday (&tv, NULL))
83 #endif
85 tv.tv_sec = time (NULL);
86 tv.tv_usec = 0;
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);
98 return BSTR (&out);
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);
116 f->max = max;
117 f->per = per;
118 f->n = 0;
119 f->reset = 0;
120 return f;
123 void
124 frequency_limit_free (struct frequency_limit *f)
126 free (f);
129 bool
130 frequency_limit_event_allowed (struct frequency_limit *f)
132 if (f->per)
134 bool ret;
135 if (now >= f->reset + f->per)
137 f->reset = now;
138 f->n = 0;
140 ret = (++f->n <= f->max);
141 return ret;
143 else
144 return true;
147 #ifdef WIN32
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)
173 time_t t;
174 mutex_lock (L_GETTIMEOFDAY);
176 QueryPerformanceFrequency((LARGE_INTEGER *) &frequency);
178 counterPerMicrosecond = (double) ((__int64) frequency) / 1000000.0f;
180 time(&t);
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);
195 return 0;
198 #endif /* WIN32 */