You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suricata/src/util-time.c

82 lines
1.8 KiB
C

16 years ago
/* Time keeping for offline (non-live) packet handling (pcap files) */
#include "eidps-common.h"
#include "detect.h"
#include "threads.h"
#include "util-debug.h"
static struct timeval current_time = { 0, 0 };
static SCMutex current_time_mutex = PTHREAD_MUTEX_INITIALIZER;
static char live = TRUE;
void TimeModeSetLive(void)
{
live = TRUE;
SCLogDebug("live time mode enabled");
}
void TimeModeSetOffline (void)
{
live = FALSE;
SCLogDebug("offline time mode enabled");
}
void TimeSet(struct timeval *tv)
{
if (live == TRUE)
return;
if (tv == NULL)
return;
SCMutexLock(&current_time_mutex);
current_time.tv_sec = tv->tv_sec;
current_time.tv_usec = tv->tv_usec;
SCLogDebug("time set to %" PRIuMAX " sec, %" PRIuMAX " usec",
(uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
SCMutexUnlock(&current_time_mutex);
}
/** \brief set the time to "gettimeofday" meant for testing */
void TimeSetToCurrentTime(void) {
struct timeval tv;
memset(&tv, 0x00, sizeof(tv));
gettimeofday(&tv, NULL);
TimeSet(&tv);
}
void TimeGet(struct timeval *tv)
{
if (tv == NULL)
return;
if (live == TRUE) {
gettimeofday(tv, NULL);
} else {
SCMutexLock(&current_time_mutex);
tv->tv_sec = current_time.tv_sec;
tv->tv_usec = current_time.tv_usec;
SCMutexUnlock(&current_time_mutex);
}
SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec",
(uintmax_t)tv->tv_sec, (uintmax_t)tv->tv_usec);
}
/** \brief increment the time in the engine
* \param tv_sec seconds to increment the time with */
void TimeSetIncrementTime(uint32_t tv_sec) {
struct timeval tv;
memset(&tv, 0x00, sizeof(tv));
TimeGet(&tv);
tv.tv_sec += tv_sec;
TimeSet(&tv);
}