aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils.c
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2009-08-31 15:03:35 +0200
committerMartin Willi <martin@strongswan.org>2009-08-31 15:03:35 +0200
commit3f310c0d1f664f5811327c5a89b5d6c2f3e42bdc (patch)
treef49413d140adadc222c553b24dea6ea2a2577fed /src/libstrongswan/utils.c
parent1d39663f7a0bfd7399ea0db24e4190bdbf196c46 (diff)
downloadstrongswan-3f310c0d1f664f5811327c5a89b5d6c2f3e42bdc.tar.bz2
strongswan-3f310c0d1f664f5811327c5a89b5d6c2f3e42bdc.tar.xz
implemented a monotonic timestamping function, unaffected from system time changes
Diffstat (limited to 'src/libstrongswan/utils.c')
-rw-r--r--src/libstrongswan/utils.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index 305841172..91242da2e 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -163,6 +163,39 @@ bool mkdir_p(const char *path, mode_t mode)
}
/**
+ * Return monotonic time
+ */
+time_t time_monotonic(timeval_t *tv)
+{
+#if defined(HAVE_CLOCK_GETTIME)
+ timespec_t ts;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
+ {
+ if (tv)
+ {
+ tv->tv_sec = ts.tv_sec;
+ tv->tv_usec = ts.tv_nsec / 1000;
+ }
+ return ts.tv_sec;
+ }
+#endif /* HAVE_CLOCK_MONOTONIC */
+ /* Fallback to non-monotonic timestamps:
+ * On MAC OS X, creating monotonic timestamps is rather difficult. We
+ * could use mach_absolute_time() and catch sleep/wakeup notifications.
+ * We stick to the simpler (non-monotonic) gettimeofday() for now. */
+ if (!tv)
+ {
+ return time(NULL);
+ }
+ if (gettimeofday(tv, NULL) != 0)
+ { /* should actually never fail if passed pointers are valid */
+ return -1;
+ }
+ return tv->tv_sec;
+}
+
+/**
* return null
*/
void *return_null()