Browse code

build: move gettimeofday() emulation to compat

Remove all references to gettimeofday() from main project.

SIDE EFFECT: mingw will use its own internal gettimeofday().

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Acked-by: David Sommerseth <davids@redhat.com>
Signed-off-by: David Sommerseth <davids@redhat.com>

Alon Bar-Lev authored on 2012/03/01 05:12:15
Showing 17 changed files
... ...
@@ -51,7 +51,6 @@
51 51
 #define HAVE_CPP_VARARG_MACRO_ISO 1
52 52
 #define HAVE_CTIME 1
53 53
 #define HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH 1
54
-#define HAVE_GETTIMEOFDAY 1
55 54
 #define HAVE_IN_PKTINFO 1
56 55
 #define HAVE_MEMSET 1
57 56
 #define HAVE_PUTENV 1
... ...
@@ -541,7 +541,6 @@ m4_define(
541 541
 	[setsockopt getsockopt getsockname poll]dnl
542 542
 )
543 543
 if test "${WIN32}" = "yes"; then
544
-	AC_DEFINE([HAVE_GETTIMEOFDAY], [1], [We fake gettimeofday for win32 at otime.c])
545 544
 	m4_foreach(
546 545
 		[F],
547 546
 		m4_split(SOCKET_FUNCS SOCKET_OPT_FUNCS),
... ...
@@ -20,4 +20,5 @@ noinst_LTLIBRARIES = libcompat.la
20 20
 libcompat_la_SOURCES = \
21 21
 	compat.h \
22 22
 	compat-dirname.c \
23
-	compat-basename.c
23
+	compat-basename.c \
24
+	compat-gettimeofday.c
24 25
new file mode 100644
... ...
@@ -0,0 +1,131 @@
0
+/*
1
+ *  OpenVPN -- An application to securely tunnel IP networks
2
+ *             over a single UDP port, with support for SSL/TLS-based
3
+ *             session authentication and key exchange,
4
+ *             packet encryption, packet authentication, and
5
+ *             packet compression.
6
+ *
7
+ *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
8
+ *
9
+ *  This program is free software; you can redistribute it and/or modify
10
+ *  it under the terms of the GNU General Public License version 2
11
+ *  as published by the Free Software Foundation.
12
+ *
13
+ *  This program is distributed in the hope that it will be useful,
14
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ *  GNU General Public License for more details.
17
+ *
18
+ *  You should have received a copy of the GNU General Public License
19
+ *  along with this program (see the file COPYING included with this
20
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
21
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
+ */
23
+
24
+#ifdef HAVE_CONFIG_H
25
+#include "config.h"
26
+#elif defined(_MSC_VER)
27
+#include "config-msvc.h"
28
+#endif
29
+
30
+#ifndef HAVE_GETTIMEOFDAY
31
+
32
+#include "compat.h"
33
+
34
+#ifdef WIN32
35
+/*
36
+ * NOTICE: mingw has much faster gettimeofday!
37
+ * autoconf will set HAVE_GETTIMEOFDAY
38
+ */
39
+
40
+#include <windows.h>
41
+#include <time.h>
42
+
43
+static time_t gtc_base = 0;
44
+static DWORD gtc_last = 0;
45
+static time_t last_sec = 0;
46
+static unsigned int last_msec = 0;
47
+static int bt_last = 0;
48
+
49
+static void
50
+gettimeofday_calibrate (void)
51
+{
52
+  const time_t t = time(NULL);
53
+  const DWORD gtc = GetTickCount();
54
+  gtc_base = t - gtc/1000;
55
+  gtc_last = gtc;
56
+}
57
+
58
+/*
59
+ * Rewritten by JY for OpenVPN 2.1, after I realized that
60
+ * QueryPerformanceCounter takes nearly 2 orders of magnitude
61
+ * more processor cycles than GetTickCount.
62
+ */
63
+int
64
+gettimeofday (struct timeval *tv, void *tz)
65
+{
66
+  const DWORD gtc = GetTickCount();
67
+  int bt = 0;
68
+  time_t sec;
69
+  unsigned int msec;
70
+  const int backtrack_hold_seconds = 10;
71
+
72
+  (void)tz;
73
+
74
+  /* recalibrate at the dreaded 49.7 day mark */
75
+  if (!gtc_base || gtc < gtc_last)
76
+    gettimeofday_calibrate ();
77
+  gtc_last = gtc;
78
+
79
+  sec = gtc_base + gtc / 1000;
80
+  msec = gtc % 1000;
81
+
82
+  if (sec == last_sec)
83
+    {
84
+      if (msec < last_msec)
85
+	{
86
+	  msec = last_msec;
87
+	  bt = 1;
88
+	}
89
+    }
90
+  else if (sec < last_sec)
91
+    {
92
+      /* We try to dampen out backtracks of less than backtrack_hold_seconds.
93
+	 Larger backtracks will be passed through and dealt with by the
94
+	 TIME_BACKTRACK_PROTECTION code (if enabled) */
95
+      if (sec > last_sec - backtrack_hold_seconds)
96
+	{
97
+	  sec = last_sec;
98
+	  msec = last_msec;
99
+	}
100
+      bt = 1;
101
+    }
102
+
103
+  tv->tv_sec = (long)last_sec = (long)sec;
104
+  tv->tv_usec = (last_msec = msec) * 1000;
105
+
106
+  if (bt && !bt_last)
107
+    gettimeofday_calibrate ();
108
+  bt_last = bt;
109
+
110
+  return 0;
111
+}
112
+
113
+#else
114
+
115
+#ifdef HAVE_TIME_H
116
+#include <time.h>
117
+#endif
118
+
119
+int
120
+gettimeofday (struct timeval *tv, void *tz)
121
+{
122
+	(void)tz;
123
+	tv->tv_sec = time(NULL);
124
+	tv->tv_usec = 0;
125
+	return 0;
126
+}
127
+
128
+#endif /* WIN32 */
129
+
130
+#endif /* HAVE_GETTIMEOFDAY */
... ...
@@ -25,6 +25,15 @@
25 25
 #ifndef COMPAT_H
26 26
 #define COMPAT_H
27 27
 
28
+#ifdef HAVE_WINSOCK2_H
29
+/* timeval */
30
+#include <winsock2.h>
31
+#endif
32
+
33
+#ifdef HAVE_SYS_TIME_H
34
+#include <sys/time.h>
35
+#endif
36
+
28 37
 #ifndef HAVE_DIRNAME
29 38
 char * dirname(char *str);
30 39
 #endif /* HAVE_DIRNAME */
... ...
@@ -33,4 +42,8 @@ char * dirname(char *str);
33 33
 char * basename(char *str);
34 34
 #endif /* HAVE_BASENAME */
35 35
 
36
+#ifndef HAVE_GETTIMEOFDAY
37
+int gettimeofday (struct timeval *tv, void *tz);
38
+#endif
39
+
36 40
 #endif /* COMPAT_H */
... ...
@@ -158,6 +158,10 @@
158 158
 				RelativePath=".\compat-dirname.c"
159 159
 				>
160 160
 			</File>
161
+			<File
162
+				RelativePath=".\compat-gettimeofday.c"
163
+				>
164
+			</File>
161 165
 		</Filter>
162 166
 		<Filter
163 167
 			Name="Header Files"
... ...
@@ -1094,7 +1094,7 @@ process_outgoing_link (struct context *c)
1094 1094
 	   * Let the traffic shaper know how many bytes
1095 1095
 	   * we wrote.
1096 1096
 	   */
1097
-#ifdef HAVE_GETTIMEOFDAY
1097
+#ifdef ENABLE_FEATURE_SHAPER
1098 1098
 	  if (c->options.shaper)
1099 1099
 	    shaper_wrote_bytes (&c->c2.shaper, BLEN (&c->c2.to_link)
1100 1100
 				+ datagram_overhead (c->options.ce.proto));
... ...
@@ -1383,7 +1383,7 @@ io_wait_dowork (struct context *c, const unsigned int flags)
1383 1383
 	   * quota, don't send -- instead compute the delay we must wait
1384 1384
 	   * until it will be OK to send the packet.
1385 1385
 	   */
1386
-#ifdef HAVE_GETTIMEOFDAY
1386
+#ifdef ENABLE_FEATURE_SHAPER
1387 1387
 	  int delay = 0;
1388 1388
 
1389 1389
 	  /* set traffic shaping delay in microseconds */
... ...
@@ -1398,9 +1398,9 @@ io_wait_dowork (struct context *c, const unsigned int flags)
1398 1398
 	    {
1399 1399
 	      shaper_soonest_event (&c->c2.timeval, delay);
1400 1400
 	    }
1401
-#else /* HAVE_GETTIMEOFDAY */
1401
+#else /* ENABLE_FEATURE_SHAPER */
1402 1402
 	  socket |= EVENT_WRITE;
1403
-#endif /* HAVE_GETTIMEOFDAY */
1403
+#endif /* ENABLE_FEATURE_SHAPER */
1404 1404
 	}
1405 1405
       else
1406 1406
 	{
... ...
@@ -1180,7 +1180,7 @@ do_init_timers (struct context *c, bool deferred)
1180 1180
 static void
1181 1181
 do_init_traffic_shaper (struct context *c)
1182 1182
 {
1183
-#ifdef HAVE_GETTIMEOFDAY
1183
+#ifdef ENABLE_FEATURE_SHAPER
1184 1184
   /* initialize traffic shaper (i.e. transmit bandwidth limiter) */
1185 1185
   if (c->options.shaper)
1186 1186
     {
... ...
@@ -3056,7 +3056,7 @@ do_setup_fast_io (struct context *c)
3056 3056
 	msg (M_INFO, "NOTE: --fast-io is disabled since we are not using UDP");
3057 3057
       else
3058 3058
 	{
3059
-#ifdef HAVE_GETTIMEOFDAY
3059
+#ifdef ENABLE_FEATURE_SHAPER
3060 3060
 	  if (c->options.shaper)
3061 3061
 	    msg (M_INFO, "NOTE: --fast-io is disabled since we are using --shaper");
3062 3062
 	  else
... ...
@@ -720,7 +720,6 @@ openvpn_popen (const struct argv *a,  const struct env_set *es)
720 720
 void
721 721
 init_random_seed(void)
722 722
 {
723
-#ifdef HAVE_GETTIMEOFDAY
724 723
   struct timeval tv;
725 724
 
726 725
   if (!gettimeofday (&tv, NULL))
... ...
@@ -728,10 +727,6 @@ init_random_seed(void)
728 728
       const unsigned int seed = (unsigned int) tv.tv_sec ^ tv.tv_usec;
729 729
       srandom (seed);
730 730
     }
731
-#else /* HAVE_GETTIMEOFDAY */
732
-  const time_t current = time (NULL);
733
-  srandom ((unsigned int)current);
734
-#endif /* HAVE_GETTIMEOFDAY */
735 731
 }
736 732
 
737 733
 /* thread-safe strerror */
... ...
@@ -269,7 +269,7 @@ struct context_2
269 269
   struct frame frame_fragment_omit;
270 270
 #endif
271 271
 
272
-#ifdef HAVE_GETTIMEOFDAY
272
+#ifdef ENABLE_FEATURE_SHAPER
273 273
   /*
274 274
    * Traffic shaper object.
275 275
    */
... ...
@@ -1455,7 +1455,7 @@ show_settings (const struct options *o)
1455 1455
   SHOW_INT (ifconfig_ipv6_netbits);
1456 1456
   SHOW_STR (ifconfig_ipv6_remote);
1457 1457
 
1458
-#ifdef HAVE_GETTIMEOFDAY
1458
+#ifdef ENABLE_FEATURE_SHAPER
1459 1459
   SHOW_INT (shaper);
1460 1460
 #endif
1461 1461
 #ifdef ENABLE_OCC
... ...
@@ -4833,7 +4833,7 @@ add_option (struct options *options,
4833 4833
     }
4834 4834
   else if (streq (p[0], "shaper") && p[1])
4835 4835
     {
4836
-#ifdef HAVE_GETTIMEOFDAY
4836
+#ifdef ENABLE_FEATURE_SHAPER
4837 4837
       int shaper;
4838 4838
 
4839 4839
       VERIFY_PERMISSION (OPT_P_SHAPER);
... ...
@@ -4845,11 +4845,11 @@ add_option (struct options *options,
4845 4845
 	  goto err;
4846 4846
 	}
4847 4847
       options->shaper = shaper;
4848
-#else /* HAVE_GETTIMEOFDAY */
4848
+#else /* ENABLE_FEATURE_SHAPER */
4849 4849
       VERIFY_PERMISSION (OPT_P_GENERAL);
4850 4850
       msg (msglevel, "--shaper requires the gettimeofday() function which is missing");
4851 4851
       goto err;
4852
-#endif /* HAVE_GETTIMEOFDAY */
4852
+#endif /* ENABLE_FEATURE_SHAPER */
4853 4853
     }
4854 4854
   else if (streq (p[0], "port") && p[1])
4855 4855
     {
... ...
@@ -260,7 +260,7 @@ struct options
260 260
   const char *ifconfig_ipv6_remote;
261 261
   bool ifconfig_noexec;
262 262
   bool ifconfig_nowarn;
263
-#ifdef HAVE_GETTIMEOFDAY
263
+#ifdef ENABLE_FEATURE_SHAPER
264 264
   int shaper;
265 265
 #endif
266 266
 
... ...
@@ -680,7 +680,7 @@ struct options
680 680
 #define ROUTE_OPTION_FLAGS(o) (0)
681 681
 #endif
682 682
 
683
-#ifdef HAVE_GETTIMEOFDAY
683
+#ifdef ENABLE_FEATURE_SHAPER
684 684
 #define SHAPER_DEFINED(opt) ((opt)->shaper)
685 685
 #else
686 686
 #define SHAPER_DEFINED(opt) (false)
... ...
@@ -36,7 +36,7 @@
36 36
 
37 37
 time_t now = 0;            /* GLOBAL */
38 38
 
39
-#if TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)
39
+#if TIME_BACKTRACK_PROTECTION
40 40
 
41 41
 static time_t now_adj = 0; /* GLOBAL */
42 42
 time_t now_usec = 0;       /* GLOBAL */
... ...
@@ -76,7 +76,7 @@ update_now_usec (struct timeval *tv)
76 76
     now_usec = tv->tv_usec;
77 77
 }
78 78
 
79
-#endif /* TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY) */
79
+#endif /* TIME_BACKTRACK_PROTECTION */
80 80
 
81 81
 /* 
82 82
  * Return a numerical string describing a struct timeval.
... ...
@@ -120,13 +120,7 @@ time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc)
120 120
     }
121 121
   else
122 122
     {
123
-#ifdef HAVE_GETTIMEOFDAY
124
-      if (gettimeofday (&tv, NULL))
125
-#endif
126
-	{
127
-	  tv.tv_sec = time (NULL);
128
-	  tv.tv_usec = 0;
129
-	}
123
+      gettimeofday (&tv, NULL);
130 124
     }
131 125
 
132 126
   t = tv.tv_sec;
... ...
@@ -185,78 +179,6 @@ frequency_limit_event_allowed (struct frequency_limit *f)
185 185
     return true;
186 186
 }
187 187
 
188
-#ifdef WIN32
189
-
190
-static time_t gtc_base = 0;
191
-static DWORD gtc_last = 0;
192
-static time_t last_sec = 0;
193
-static unsigned int last_msec = 0;
194
-static bool bt_last = false;
195
-
196
-static void
197
-gettimeofday_calibrate (void)
198
-{
199
-  const time_t t = time(NULL);
200
-  const DWORD gtc = GetTickCount();
201
-  gtc_base = t - gtc/1000;
202
-  gtc_last = gtc;
203
-}
204
-
205
-/*
206
- * Rewritten by JY for OpenVPN 2.1, after I realized that
207
- * QueryPerformanceCounter takes nearly 2 orders of magnitude
208
- * more processor cycles than GetTickCount.
209
- */
210
-int
211
-gettimeofday (struct timeval *tv, void *tz)
212
-{
213
-  const DWORD gtc = GetTickCount();
214
-  bool bt = false;
215
-  time_t sec;
216
-  unsigned int msec;
217
-  const int backtrack_hold_seconds = 10;
218
-
219
-  /* recalibrate at the dreaded 49.7 day mark */
220
-  if (!gtc_base || gtc < gtc_last)
221
-    gettimeofday_calibrate ();
222
-  gtc_last = gtc;
223
-
224
-  sec = gtc_base + gtc / 1000;
225
-  msec = gtc % 1000;
226
-
227
-  if (sec == last_sec)
228
-    {
229
-      if (msec < last_msec)
230
-	{
231
-	  msec = last_msec;
232
-	  bt = true;
233
-	}
234
-    }
235
-  else if (sec < last_sec)
236
-    {
237
-      /* We try to dampen out backtracks of less than backtrack_hold_seconds.
238
-	 Larger backtracks will be passed through and dealt with by the
239
-	 TIME_BACKTRACK_PROTECTION code (if enabled) */
240
-      if (sec > last_sec - backtrack_hold_seconds)
241
-	{
242
-	  sec = last_sec;
243
-	  msec = last_msec;
244
-	}
245
-      bt = true;
246
-    }
247
-
248
-  tv->tv_sec = last_sec = sec;
249
-  tv->tv_usec = (last_msec = msec) * 1000;
250
-
251
-  if (bt && !bt_last)
252
-    gettimeofday_calibrate ();
253
-  bt_last = bt;
254
-
255
-  return 0;
256
-}
257
-
258
-#endif /* WIN32 */
259
-
260 188
 #ifdef TIME_TEST
261 189
 void
262 190
 time_test (void)
... ...
@@ -41,10 +41,6 @@ struct frequency_limit *frequency_limit_init (int max, int per);
41 41
 void frequency_limit_free (struct frequency_limit *f);
42 42
 bool frequency_limit_event_allowed (struct frequency_limit *f);
43 43
 
44
-#ifdef WIN32
45
-int gettimeofday(struct timeval *tv, void *tz);
46
-#endif
47
-
48 44
 /* format a time_t as ascii, or use current time if 0 */
49 45
 const char* time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc);
50 46
 
... ...
@@ -57,7 +53,7 @@ extern time_t now; /* updated frequently to time(NULL) */
57 57
 
58 58
 void time_test (void);
59 59
 
60
-#if TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)
60
+#if TIME_BACKTRACK_PROTECTION
61 61
 
62 62
 void update_now (const time_t system_time);
63 63
 
... ...
@@ -89,12 +85,13 @@ update_time (void)
89 89
 #endif
90 90
 }
91 91
 
92
-#else /* !(TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)) */
92
+#else /* !TIME_BACKTRACK_PROTECTION */
93 93
 
94 94
 static inline void
95 95
 update_time (void)
96 96
 {
97
-#if defined(WIN32) && defined(HAVE_GETTIMEOFDAY)
97
+#if defined(WIN32)
98
+  /* on WIN32, gettimeofday is faster than time(NULL) */
98 99
   struct timeval tv;
99 100
   if (!gettimeofday (&tv, NULL))
100 101
     {
... ...
@@ -108,17 +105,13 @@ update_time (void)
108 108
 #endif
109 109
 }
110 110
 
111
-#ifdef HAVE_GETTIMEOFDAY
112
-
113 111
 static inline int
114 112
 openvpn_gettimeofday (struct timeval *tv, void *tz)
115 113
 {
116 114
   return gettimeofday (tv, tz);
117 115
 }
118 116
 
119
-#endif
120
-
121
-#endif /* TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY) */
117
+#endif /* TIME_BACKTRACK_PROTECTION */
122 118
 
123 119
 static inline time_t
124 120
 openvpn_time (time_t *t)
... ...
@@ -32,7 +32,7 @@
32 32
 #include "shaper.h"
33 33
 #include "memdbg.h"
34 34
 
35
-#ifdef HAVE_GETTIMEOFDAY
35
+#ifdef ENABLE_FEATURE_SHAPER
36 36
 
37 37
 /*
38 38
  * We want to wake up in delay microseconds.  If timeval is larger
... ...
@@ -98,4 +98,4 @@ shaper_msg (struct shaper *s)
98 98
 
99 99
 #else
100 100
 static void dummy(void) {}
101
-#endif /* HAVE_GETTIMEOFDAY */
101
+#endif /* ENABLE_FEATURE_SHAPER */
... ...
@@ -27,7 +27,7 @@
27 27
 
28 28
 /*#define SHAPER_DEBUG*/
29 29
 
30
-#ifdef HAVE_GETTIMEOFDAY
30
+#ifdef ENABLE_FEATURE_SHAPER
31 31
 
32 32
 #include "basic.h"
33 33
 #include "integer.h"
... ...
@@ -173,6 +173,6 @@ shaper_change_pct (struct shaper *s, int pct)
173 173
 }
174 174
 #endif
175 175
 
176
-#endif /* HAVE_GETTIMEOFDAY */
176
+#endif /* ENABLE_FEATURE_SHAPER */
177 177
 
178 178
 #endif
... ...
@@ -383,6 +383,13 @@
383 383
 #endif
384 384
 
385 385
 /*
386
+ * Do we have nanoseconds gettimeofday?
387
+ */
388
+#if defined(HAVE_GETTIMEOFDAY) || defined(WIN32)
389
+#define HAVE_GETTIMEOFDAY_NANOSECONDS 1
390
+#endif
391
+
392
+/*
386 393
  * Do we have the capability to report extended socket errors?
387 394
  */
388 395
 #if defined(HAVE_LINUX_TYPES_H) && defined(HAVE_LINUX_ERRQUEUE_H) && defined(HAVE_SOCK_EXTENDED_ERR) && defined(HAVE_MSGHDR) && defined(HAVE_CMSGHDR) && defined(CMSG_FIRSTHDR) && defined(CMSG_NXTHDR) && defined(IP_RECVERR) && defined(MSG_ERRQUEUE) && defined(SOL_IP) && defined(HAVE_IOVEC)
... ...
@@ -486,7 +493,7 @@ socket_defined (const socket_descriptor_t sd)
486 486
  * Do we have point-to-multipoint capability?
487 487
  */
488 488
 
489
-#if defined(ENABLE_CLIENT_SERVER) && defined(ENABLE_CRYPTO) && defined(ENABLE_SSL) && defined(HAVE_GETTIMEOFDAY)
489
+#if defined(ENABLE_CLIENT_SERVER) && defined(ENABLE_CRYPTO) && defined(ENABLE_SSL) && defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
490 490
 #define P2MP 1
491 491
 #else
492 492
 #define P2MP 0
... ...
@@ -667,7 +674,16 @@ socket_defined (const socket_descriptor_t sd)
667 667
  * Reduce sensitivity to system clock instability
668 668
  * and backtracks.
669 669
  */
670
+#if defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
670 671
 #define TIME_BACKTRACK_PROTECTION 1
672
+#endif
673
+
674
+/*
675
+ * Enable traffic shaper.
676
+ */
677
+#if defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
678
+#define ENABLE_FEATURE_SHAPER 1
679
+#endif
671 680
 
672 681
 /*
673 682
  * Is non-blocking connect() supported?