summaryrefslogtreecommitdiffstats
path: root/otime.c
diff options
context:
space:
mode:
authorjames <james@e7ae566f-a301-0410-adde-c780ea21d3b5>2005-11-12 10:59:41 +0000
committerjames <james@e7ae566f-a301-0410-adde-c780ea21d3b5>2005-11-12 10:59:41 +0000
commit0475d17e1ce34e6b2471d17a102b7c2a2d1427c7 (patch)
tree33e9d066a65da9025ecabd2f42f1612c1fd11e7d /otime.c
parentf19f12c8b90bfefc716c6e359bc10ab1a53c74d8 (diff)
downloadopenvpn-0475d17e1ce34e6b2471d17a102b7c2a2d1427c7.tar.gz
openvpn-0475d17e1ce34e6b2471d17a102b7c2a2d1427c7.tar.xz
openvpn-0475d17e1ce34e6b2471d17a102b7c2a2d1427c7.zip
Reduce sensitivity to system clock instability
and backtracks. git-svn-id: http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn@799 e7ae566f-a301-0410-adde-c780ea21d3b5
Diffstat (limited to 'otime.c')
-rw-r--r--otime.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/otime.c b/otime.c
index ad9cbbf..fbef02d 100644
--- a/otime.c
+++ b/otime.c
@@ -34,7 +34,54 @@
#include "memdbg.h"
-volatile time_t now; /* GLOBAL */
+time_t now = 0; /* GLOBAL */
+
+#if TIME_BACKTRACK_PROTECTION
+
+static time_t now_adj = 0; /* GLOBAL */
+
+/*
+ * Try to filter out time instability caused by the system
+ * clock backtracking or jumping forward.
+ */
+
+void
+update_now (const time_t system_time)
+{
+ const int threshold = 86400; /* threshold at which to dampen forward jumps */
+ time_t real_time = system_time + now_adj;
+ if (real_time > now)
+ {
+ const time_t overshoot = real_time - now - 1;
+ if (overshoot > threshold && now_adj >= overshoot)
+ {
+ now_adj -= overshoot;
+ real_time -= overshoot;
+ }
+ now = real_time;
+ }
+ else if (real_time < now)
+ {
+ now_adj += (now - real_time);
+ }
+}
+
+#ifdef HAVE_GETTIMEOFDAY
+
+time_t now_usec = 0; /* GLOBAL */
+
+void
+update_now_usec (struct timeval *tv)
+{
+ const time_t last = now;
+ update_now (tv->tv_sec);
+ if (now > last || tv->tv_usec > now_usec)
+ now_usec = tv->tv_usec;
+}
+
+#endif
+
+#endif
/*
* Return a numerical string describing a struct timeval.