summaryrefslogtreecommitdiff
path: root/src/libstrongswan/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/utils.c')
-rw-r--r--src/libstrongswan/utils.c70
1 files changed, 54 insertions, 16 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index 305841172..e331ac0d0 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -50,9 +50,9 @@ void *clalloc(void * pointer, size_t size)
{
void *data;
data = malloc(size);
-
+
memcpy(data, pointer, size);
-
+
return (data);
}
@@ -62,9 +62,9 @@ void *clalloc(void * pointer, size_t size)
void memxor(u_int8_t dst[], u_int8_t src[], size_t n)
{
int m, i;
-
+
/* byte wise XOR until dst aligned */
- for (i = 0; (uintptr_t)&dst[i] % sizeof(long); i++)
+ for (i = 0; (uintptr_t)&dst[i] % sizeof(long) && i < n; i++)
{
dst[i] ^= src[i];
}
@@ -163,6 +163,44 @@ bool mkdir_p(const char *path, mode_t mode)
}
/**
+ * Return monotonic time
+ */
+time_t time_monotonic(timeval_t *tv)
+{
+#if defined(HAVE_CLOCK_GETTIME) && \
+ (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \
+ defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
+ /* as we use time_monotonic() for condvar operations, we use the
+ * monotonic time source only if it is also supported by pthread. */
+ 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_GETTIME && (...) */
+ /* 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.
+ * But keep in mind: we need the same time source here as in condvar! */
+ 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()
@@ -197,7 +235,7 @@ void nop()
#include <pthread.h>
/**
- * We use a single mutex for all refcount variables.
+ * We use a single mutex for all refcount variables.
*/
static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -217,7 +255,7 @@ void ref_get(refcount_t *ref)
bool ref_put(refcount_t *ref)
{
bool more_refs;
-
+
pthread_mutex_lock(&ref_mutex);
more_refs = --(*ref);
pthread_mutex_unlock(&ref_mutex);
@@ -238,7 +276,7 @@ int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
time_t *time = *((time_t**)(args[0]));
bool utc = *((bool*)(args[1]));;
struct tm t;
-
+
if (time == UNDEFINED_TIME)
{
return print_in_hook(dst, len, "--- -- --:--:--%s----",
@@ -267,7 +305,7 @@ int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
time_t *arg1 = *((time_t**)(args[0]));
time_t *arg2 = *((time_t**)(args[1]));
time_t delta = abs(*arg1 - *arg2);
-
+
if (delta > 2 * 60 * 60 * 24)
{
delta /= 60 * 60 * 24;
@@ -301,7 +339,7 @@ int mem_printf_hook(char *dst, size_t dstlen,
{
char *bytes = *((void**)(args[0]));
int len = *((size_t*)(args[1]));
-
+
char buffer[BYTES_PER_LINE * 3];
char ascii_buffer[BYTES_PER_LINE + 1];
char *buffer_pos = buffer;
@@ -310,9 +348,9 @@ int mem_printf_hook(char *dst, size_t dstlen,
int line_start = 0;
int i = 0;
int written = 0;
-
+
written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes);
-
+
while (bytes_pos < bytes_roof)
{
*buffer_pos++ = hexdig_upper[(*bytes_pos >> 4) & 0xF];
@@ -321,20 +359,20 @@ int mem_printf_hook(char *dst, size_t dstlen,
ascii_buffer[i++] =
(*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
- if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
+ if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
{
int padding = 3 * (BYTES_PER_LINE - i);
-
+
while (padding--)
{
*buffer_pos++ = ' ';
}
*buffer_pos++ = '\0';
ascii_buffer[i] = '\0';
-
+
written += print_in_hook(dst, dstlen, "\n%4d: %s %s",
- line_start, buffer, ascii_buffer);
-
+ line_start, buffer, ascii_buffer);
+
buffer_pos = buffer;
line_start += BYTES_PER_LINE;
i = 0;