suricata
util-time.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Ken Steele <suricata@tilera.com>
23  *
24  * Time keeping for offline (non-live) packet handling (pcap files).
25  * And time string generation for alerts.
26  */
27 
28 /* Real time vs offline time
29  *
30  * When we run on live traffic, time handling is simple. Packets have a
31  * timestamp set by the capture method. Management threads can simply
32  * use 'gettimeofday' to know the current time. There should never be
33  * any serious gap between the two.
34  *
35  * In offline mode, things are dramatically different. Here we try to keep
36  * the time from the pcap, which means that if the packets are in 2011 the
37  * log output should also reflect this. Multiple issues:
38  * 1. merged pcaps might have huge time jumps or time going backward
39  * 2. slowly recorded pcaps may be processed much faster than their 'realtime'
40  * 3. management threads need a concept of what the 'current' time is for
41  * enforcing timeouts
42  * 4. due to (1) individual threads may have very different views on what
43  * the current time is. E.g. T1 processed packet 1 with TS X, while T2
44  * at the very same time processes packet 2 with TS X+100000s.
45  *
46  * In offline mode we keep the timestamp per thread. If a management thread
47  * needs current time, it will get the minimum of the threads' values. This
48  * is to avoid the problem that T2s time value might already trigger a flow
49  * timeout as the flow lastts + 100000s is almost certainly meaning the flow
50  * would be considered timed out.
51  */
52 
53 #ifdef OS_WIN32
54 /* for MinGW we need to set _POSIX_C_SOURCE before including
55  * sys/time.h. */
56 #ifndef _POSIX_C_SOURCE
57 #define _POSIX_C_SOURCE 200809L
58 #endif
59 #endif
60 
61 #include "suricata-common.h"
62 #include "suricata.h"
63 #include "detect.h"
64 #include "threads.h"
65 #include "tm-threads.h"
66 #include "util-debug.h"
67 #include "util-time.h"
68 
69 #ifdef UNITTESTS
70 static struct timeval current_time = { 0, 0 };
71 #endif
72 //static SCMutex current_time_mutex = SCMUTEX_INITIALIZER;
73 static SCSpinlock current_time_spinlock;
74 static bool live_time_tracking = true;
75 
76 struct tm *SCLocalTime(time_t timep, struct tm *result);
77 struct tm *SCUtcTime(time_t timep, struct tm *result);
78 
79 void TimeInit(void)
80 {
81  SCSpinInit(&current_time_spinlock, 0);
82 
83  /* Initialize Time Zone settings. */
84  tzset();
85 }
86 
87 void TimeDeinit(void)
88 {
89  SCSpinDestroy(&current_time_spinlock);
90 }
91 
92 bool TimeModeIsReady(void)
93 {
94  if (live_time_tracking)
95  return true;
97 }
98 
99 void TimeModeSetLive(void)
100 {
101  live_time_tracking = true;
102  SCLogDebug("live time mode enabled");
103 }
104 
106 {
107  live_time_tracking = false;
108  SCLogDebug("offline time mode enabled");
109 }
110 
111 bool TimeModeIsLive(void)
112 {
113  return live_time_tracking;
114 }
115 
116 void TimeSetByThread(const int thread_id, SCTime_t tv)
117 {
118  if (live_time_tracking)
119  return;
120 
121  TmThreadsSetThreadTimestamp(thread_id, tv);
122 }
123 
124 #ifdef UNITTESTS
126 {
127  if (live_time_tracking)
128  return;
129 
130  SCSpinLock(&current_time_spinlock);
131  SCTIME_TO_TIMEVAL(&current_time, ts);
132 
133  SCLogDebug("time set to %" PRIuMAX " sec, %" PRIuMAX " usec",
134  (uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
135 
136  SCSpinUnlock(&current_time_spinlock);
137 }
138 
139 /** \brief set the time to "gettimeofday" meant for testing */
141 {
142  struct timeval tv;
143  memset(&tv, 0x00, sizeof(tv));
144 
145  gettimeofday(&tv, NULL);
146 
148  TimeSet(ts);
149 }
150 #endif
151 
153 {
154  struct timeval tv = { 0 };
155  if (live_time_tracking) {
156  gettimeofday(&tv, NULL);
157  } else {
158 #ifdef UNITTESTS
159  if (unlikely(RunmodeIsUnittests())) {
160  SCSpinLock(&current_time_spinlock);
161  tv.tv_sec = current_time.tv_sec;
162  tv.tv_usec = current_time.tv_usec;
163  SCSpinUnlock(&current_time_spinlock);
164  } else {
165 #endif
167 #ifdef UNITTESTS
168  }
169 #endif
170  }
171 
172  SCLogDebug("time we got is %" PRIuMAX " sec, %" PRIuMAX " usec", (uintmax_t)tv.tv_sec,
173  (uintmax_t)tv.tv_usec);
174  return SCTIME_FROM_TIMEVAL(&tv);
175 }
176 
177 #ifdef UNITTESTS
178 /** \brief increment the time in the engine
179  * \param tv_sec seconds to increment the time with */
180 void TimeSetIncrementTime(uint32_t tv_sec)
181 {
182  SCTime_t ts = TimeGet();
183 
184  ts = SCTIME_ADD_SECS(ts, tv_sec);
185 
186  TimeSet(ts);
187 }
188 #endif
189 
190 #ifdef OS_WIN32
191 /** \internal
192  * \brief wrapper around strftime on Windows to provide output
193  * compatible with posix %z
194  */
195 static inline void WinStrftime(const SCTime_t ts, const struct tm *t, char *str, size_t size)
196 {
197  char time_fmt[64] = { 0 };
198  char tz[6] = { 0 };
199  const long int tzdiff = -_timezone;
200  const int h = abs(_timezone) / 3600 + _daylight;
201  const int m = (abs(_timezone) % 3600) / 60;
202  snprintf(tz, sizeof(tz), "%c%02d%02d", tzdiff < 0 ? '-' : '+', h, m);
203  strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S.%%06u", t);
204  snprintf(str, size, time_fmt, SCTIME_USECS(ts));
205  strlcat(str, tz, size); // append our timezone
206 }
207 #endif
208 
209 void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
210 {
211  time_t time = SCTIME_SECS(ts);
212  struct tm local_tm;
213  memset(&local_tm, 0, sizeof(local_tm));
214  struct tm *t = (struct tm*)SCLocalTime(time, &local_tm);
215 
216  if (likely(t != NULL)) {
217 #ifdef OS_WIN32
218  WinStrftime(ts, t, str, size);
219 #else
220  char time_fmt[64] = { 0 };
221  int64_t usec = SCTIME_USECS(ts);
222  strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S.%%06" PRIi64 "%z", t);
223  snprintf(str, size, time_fmt, usec);
224 #endif
225  } else {
226  snprintf(str, size, "ts-error");
227  }
228 }
229 
230 void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size)
231 {
232  time_t time = SCTIME_SECS(ts);
233  struct tm local_tm;
234  memset(&local_tm, 0, sizeof(local_tm));
235  struct tm *t = (struct tm*)SCUtcTime(time, &local_tm);
236 
237  if (likely(t != NULL)) {
238  char time_fmt[64] = { 0 };
239  strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S", t);
240  snprintf(str, size, time_fmt, SCTIME_USECS(ts));
241  } else {
242  snprintf(str, size, "ts-error");
243  }
244 }
245 
246 void CreateFormattedTimeString (const struct tm *t, const char *fmt, char *str, size_t size)
247 {
248  if (likely(t != NULL)) {
249  strftime(str, size, fmt, t);
250  } else {
251  snprintf(str, size, "ts-error");
252  }
253 }
254 
255 struct tm *SCUtcTime(time_t timep, struct tm *result)
256 {
257  return gmtime_r(&timep, result);
258 }
259 
260 /*
261  * Time Caching code
262  */
263 
264 #ifndef TLS
265 /* OpenBSD does not support thread_local, so don't use time caching on BSD
266  */
267 struct tm *SCLocalTime(time_t timep, struct tm *result)
268 {
269  return localtime_r(&timep, result);
270 }
271 
272 void CreateTimeString(const SCTime_t ts, char *str, size_t size)
273 {
274  time_t time = SCTIME_SECS(ts);
275  struct tm local_tm;
276  struct tm *t = (struct tm*)SCLocalTime(time, &local_tm);
277 
278  if (likely(t != NULL)) {
279  snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", t->tm_mon + 1, t->tm_mday,
280  t->tm_year + 1900, t->tm_hour, t->tm_min, t->tm_sec, (uint32_t)SCTIME_USECS(ts));
281  } else {
282  snprintf(str, size, "ts-error");
283  }
284 }
285 
286 #else
287 
288 /* On systems supporting thread_local, use Per-thread values for caching
289  * in CreateTimeString */
290 
291 /* The maximum possible length of the time string.
292  * "%02d/%02d/%02d-%02d:%02d:%02d.%06u"
293  * Or "01/01/2013-15:42:21.123456", which is 26, so round up to 32. */
294 #define MAX_LOCAL_TIME_STRING 32
295 
296 static thread_local int mru_time_slot; /* Most recently used cached value */
297 static thread_local time_t last_local_time[2];
298 static thread_local short int cached_local_time_len[2];
299 static thread_local char cached_local_time[2][MAX_LOCAL_TIME_STRING];
300 
301 /* Per-thread values for caching SCLocalTime() These cached values are
302  * independent from the CreateTimeString cached values. */
303 static thread_local int mru_tm_slot; /* Most recently used local tm */
304 static thread_local time_t cached_minute_start[2];
305 static thread_local struct tm cached_local_tm[2];
306 
307 /** \brief Convert time_t into Year, month, day, hour and minutes.
308  * \param timep Time in seconds since defined date.
309  * \param result The structure into which the broken down time it put.
310  *
311  * To convert a time in seconds into year, month, day, hours, minutes
312  * and seconds, call localtime_r(), which uses the current time zone
313  * to compute these values. Note, glibc's localtime_r() acquires a lock
314  * each time it is called, which limits parallelism. To call
315  * localtime_r() less often, the values returned are cached for the
316  * current and previous minute and then seconds are adjusted to
317  * compute the returned result. This is valid as long as the
318  * difference between the start of the current minute and the current
319  * time is less than 60 seconds. Once the minute value changes, all
320  * the other values could change.
321  *
322  * Two values are cached to prevent thrashing when changing from one
323  * minute to the next. The two cached minutes are independent and are
324  * not required to be M and M+1. If more than two minutes are
325  * requested, the least-recently-used cached value is updated more
326  * often, the results are still correct, but performance will be closer
327  * to previous performance.
328  */
329 struct tm *SCLocalTime(time_t timep, struct tm *result)
330 {
331  /* Only get a new local time when the time crosses into a new
332  * minute. */
333  int mru = mru_tm_slot;
334  int lru = 1 - mru;
335  int mru_seconds = timep - cached_minute_start[mru];
336  int lru_seconds = timep - cached_minute_start[lru];
337  int new_seconds;
338  if (cached_minute_start[mru]==0 && cached_minute_start[lru]==0) {
339  localtime_r(&timep, &cached_local_tm[lru]);
340  /* Subtract seconds to get back to the start of the minute. */
341  new_seconds = cached_local_tm[lru].tm_sec;
342  cached_minute_start[lru] = timep - new_seconds;
343  mru = lru;
344  mru_tm_slot = mru;
345  } else if (lru_seconds > 0 && (mru_seconds >= 0 && mru_seconds <= 59)) {
346  /* Use most-recently cached time, adjusting the seconds. */
347  new_seconds = mru_seconds;
348  } else if (mru_seconds > 0 && (lru_seconds >= 0 && lru_seconds <= 59)) {
349  /* Use least-recently cached time, update to most recently used. */
350  new_seconds = lru_seconds;
351  mru = lru;
352  mru_tm_slot = mru;
353  } else {
354  /* Update least-recent cached time. */
355  if (localtime_r(&timep, &cached_local_tm[lru]) == NULL)
356  return NULL;
357  /* Subtract seconds to get back to the start of the minute. */
358  new_seconds = cached_local_tm[lru].tm_sec;
359  cached_minute_start[lru] = timep - new_seconds;
360  mru = lru;
361  mru_tm_slot = mru;
362  }
363  memcpy(result, &cached_local_tm[mru], sizeof(struct tm));
364  result->tm_sec = new_seconds;
365 
366  return result;
367 }
368 
369 /* Update the cached time string in cache index N, for the current minute. */
370 static int UpdateCachedTime(int n, time_t time)
371 {
372  struct tm local_tm;
373  struct tm *t = (struct tm *)SCLocalTime(time, &local_tm);
374  int cached_len = snprintf(cached_local_time[n], MAX_LOCAL_TIME_STRING,
375  "%02d/%02d/%02d-%02d:%02d:",
376  t->tm_mon + 1, t->tm_mday, t->tm_year + 1900,
377  t->tm_hour, t->tm_min);
378  cached_local_time_len[n] = cached_len;
379  /* Store the time of the beginning of the minute. */
380  last_local_time[n] = time - t->tm_sec;
381  mru_time_slot = n;
382 
383  return t->tm_sec;
384 }
385 
386 /** \brief Return a formatted string for the provided time.
387  *
388  * Cache the Month/Day/Year - Hours:Min part of the time string for
389  * the current minute. Copy that result into the return string and
390  * then only print the seconds for each call.
391  */
392 void CreateTimeString(const SCTime_t ts, char *str, size_t size)
393 {
394  time_t time = SCTIME_SECS(ts);
395  int seconds;
396 
397  /* Only get a new local time when the time crosses into a new
398  * minute */
399  int mru = mru_time_slot;
400  int lru = 1 - mru;
401  int mru_seconds = time - last_local_time[mru];
402  int lru_seconds = time - last_local_time[lru];
403  if (last_local_time[mru]==0 && last_local_time[lru]==0) {
404  /* First time here, update both caches */
405  UpdateCachedTime(mru, time);
406  seconds = UpdateCachedTime(lru, time);
407  } else if (mru_seconds >= 0 && mru_seconds <= 59) {
408  /* Use most-recently cached time. */
409  seconds = mru_seconds;
410  } else if (lru_seconds >= 0 && lru_seconds <= 59) {
411  /* Use least-recently cached time. Change this slot to Most-recent */
412  seconds = lru_seconds;
413  mru_time_slot = lru;
414  } else {
415  /* Update least-recent cached time. Lock accessing local time
416  * function because it keeps any internal non-spin lock. */
417  seconds = UpdateCachedTime(lru, time);
418  }
419 
420  /* Copy the string up to the current minute then print the seconds
421  into the return string buffer. */
422  char *cached_str = cached_local_time[mru_time_slot];
423  int cached_len = cached_local_time_len[mru_time_slot];
424  if (cached_len >= (int)size)
425  cached_len = size;
426  memcpy(str, cached_str, cached_len);
427  snprintf(str + cached_len, size - cached_len, "%02d.%06u", seconds, (uint32_t)SCTIME_USECS(ts));
428 }
429 
430 #endif /* defined(__OpenBSD__) */
431 
432 #ifdef UNITTESTS
433 /** \brief Forget the local times cached by the calling thread, e.g. after the
434  * timezone was changed */
436 {
437 #ifdef TLS
438  memset(last_local_time, 0, sizeof(last_local_time));
439  memset(cached_minute_start, 0, sizeof(cached_minute_start));
440 #endif
441 }
442 #endif
443 
444 /**
445  * \brief Convert broken-down time to seconds since Unix epoch.
446  *
447  * This function is based on: http://www.catb.org/esr/time-programming
448  * (released to the public domain).
449  *
450  * \param tp Pointer to broken-down time.
451  *
452  * \retval Seconds since Unix epoch.
453  */
454 time_t SCMkTimeUtc (struct tm *tp)
455 {
456  time_t result;
457  long year;
458 #define MONTHSPERYEAR 12
459  static const int mdays[MONTHSPERYEAR] =
460  { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
461 
462  year = 1900 + tp->tm_year + tp->tm_mon / MONTHSPERYEAR;
463  result = (year - 1970) * 365 + mdays[tp->tm_mon % MONTHSPERYEAR];
464  result += (year - 1968) / 4;
465  result -= (year - 1900) / 100;
466  result += (year - 1600) / 400;
467  if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
468  (tp->tm_mon % MONTHSPERYEAR) < 2)
469  result--;
470  result += tp->tm_mday - 1;
471  result *= 24;
472  result += tp->tm_hour;
473  result *= 60;
474  result += tp->tm_min;
475  result *= 60;
476  result += tp->tm_sec;
477 #if !defined(OS_WIN32) && !defined(__sun)
478  if (tp->tm_gmtoff)
479  result -= tp->tm_gmtoff;
480 #endif
481  return result;
482 }
483 
484 /**
485  * \brief Parse a date string based on specified patterns.
486  *
487  * This function is based on GNU C library getdate.
488  *
489  * \param string Date string to parse.
490  * \param patterns String array containing patterns.
491  * \param num_patterns Number of patterns to check.
492  * \param tp Pointer to broken-down time.
493  *
494  * \retval 0 on success.
495  * \retval 1 on failure.
496  */
497 int SCStringPatternToTime (char *string, const char **patterns, int num_patterns,
498  struct tm *tp)
499 {
500  char *result = NULL;
501  int i = 0;
502 
503  /* Do the pattern matching */
504  for (i = 0; i < num_patterns; i++)
505  {
506  if (patterns[i] == NULL)
507  continue;
508 
509  tp->tm_hour = tp->tm_min = tp->tm_sec = 0;
510  tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
511  tp->tm_isdst = -1;
512 #if !defined(OS_WIN32) && !defined(__sun)
513  tp->tm_gmtoff = 0;
514  tp->tm_zone = NULL;
515 #endif
516  result = strptime(string, patterns[i], tp);
517 
518  if (result && *result == '\0')
519  break;
520  }
521 
522  /* Return if no patterns matched */
523  if (result == NULL || *result != '\0')
524  return 1;
525 
526  /* Return if no date is given */
527  if (tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN &&
528  tp->tm_mday == INT_MIN)
529  return 1;
530 
531  /* The first of the month is assumed, if only year and
532  month is given */
533  if (tp->tm_year != INT_MIN && tp->tm_mon != INT_MIN &&
534  tp->tm_mday <= 0)
535  tp->tm_mday = 1;
536 
537  /* The first date of the year is assumed, if only year
538  is given */
539  if (tp->tm_year != INT_MIN && tp->tm_mon <= 0 && tp->tm_mday <= 0) {
540  tp->tm_mday = 1;
541  tp->tm_mon = 1;
542  }
543  return 0;
544 }
545 
546 /**
547  * \brief Convert epoch time to string pattern.
548  *
549  * This function converts epoch time to a string based on a pattern.
550  *
551  * \param epoch Epoch time.
552  * \param pattern String pattern.
553  * \param str Formated string.
554  * \param size Size of allocated string.
555  *
556  * \retval 0 on success.
557  * \retval 1 on failure.
558  */
559 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str, size_t size)
560 {
561  struct tm tm;
562  memset(&tm, 0, sizeof(tm));
563  struct tm *tp = (struct tm *)SCLocalTime(epoch, &tm);
564  char buffer[PATH_MAX] = { 0 };
565 
566  if (unlikely(tp == NULL)) {
567  return 1;
568  }
569 
570  size_t r = strftime(buffer, sizeof(buffer), pattern, tp);
571  if (r == 0) {
572  return 1;
573  }
574 
575  strlcpy(str, buffer, size);
576 
577  return 0;
578 }
579 
580 /**
581  * \brief Parse string containing time size (1m, 1h, etc).
582  *
583  * \param str String to parse.
584  *
585  * \retval size on success.
586  * \retval 0 on failure.
587  */
588 uint64_t SCParseTimeSizeString (const char *str)
589 {
590  uint64_t size = 0;
591  uint64_t modifier = 1;
592  char last = str[strlen(str)-1];
593 
594  switch (last)
595  {
596  case '0' ... '9':
597  break;
598  /* seconds */
599  case 's':
600  break;
601  /* minutes */
602  case 'm':
603  modifier = 60;
604  break;
605  /* hours */
606  case 'h':
607  modifier = 60 * 60;
608  break;
609  /* days */
610  case 'd':
611  modifier = 60 * 60 * 24;
612  break;
613  /* weeks */
614  case 'w':
615  modifier = 60 * 60 * 24 * 7;
616  break;
617  /* invalid */
618  default:
619  return 0;
620  }
621 
622  errno = 0;
623  size = strtoumax(str, NULL, 10);
624  if (errno) {
625  return 0;
626  }
627 
628  return (size * modifier);
629 }
630 
631 uint64_t SCTimespecAsEpochMillis(const struct timespec* ts)
632 {
633  return ts->tv_sec * 1000L + ts->tv_nsec / 1000000L;
634 }
635 
636 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
637 {
638  return (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000L + (t1.tv_usec - t0.tv_usec);
639 }
640 
641 #ifdef UNITTESTS
642 static int CreateFormattedTimeStringTest01(void)
643 {
644  /* strftime, which underpins CreateFormattedTimeString, does not seem to
645  * function properly on MinGW. */
646 #ifndef __MINGW32__
647  struct tm tm;
648  tm.tm_sec = 0;
649  tm.tm_min = 30;
650  tm.tm_hour = 4;
651  tm.tm_mday = 13;
652  tm.tm_mon = 0;
653  tm.tm_year = 114;
654  tm.tm_wday = 1;
655  tm.tm_yday = 13;
656  tm.tm_isdst = -1;
657 
658  /* mktime() interprets the broken-down time as local time and SCLocalTime()
659  * converts it back, so the result is independent of the active timezone.
660  * The CI runs this test under several timezones to guard that round trip. */
661  struct tm local_tm;
662  struct tm *t = SCLocalTime(mktime(&tm), &local_tm);
663 
664  char buf[128] = { 0 };
665  CreateFormattedTimeString(t, "%m/%d/%y-%H:%M:%S", buf, sizeof(buf));
666 
667  FAIL_IF(strcmp(buf, "01/13/14-04:30:00") != 0);
668 #endif
669  PASS;
670 }
671 #endif /* UNITTESTS */
672 
674 {
675 #ifdef UNITTESTS
676  UtRegisterTest("CreateFormattedTimeStringTest01", CreateFormattedTimeStringTest01);
677 #endif
678 }
SCParseTimeSizeString
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
Definition: util-time.c:588
TmThreadsTimeSubsysIsReady
bool TmThreadsTimeSubsysIsReady(void)
Definition: tm-threads.c:2306
tm-threads.h
ts
uint64_t ts
Definition: source-erf-file.c:68
SCSpinDestroy
#define SCSpinDestroy
Definition: threads-debug.h:240
CreateIsoTimeString
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:209
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
CreateFormattedTimeString
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
Definition: util-time.c:246
threads.h
SCMkTimeUtc
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
Definition: util-time.c:454
SCSpinLock
#define SCSpinLock
Definition: threads-debug.h:236
m
SCMutex m
Definition: flow-hash.h:6
TmThreadsGetMinimalTimestamp
void TmThreadsGetMinimalTimestamp(struct timeval *ts)
Definition: tm-threads.c:2357
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
SCTimeToStringPattern
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
Definition: util-time.c:559
SCUtcTime
struct tm * SCUtcTime(time_t timep, struct tm *result)
Definition: util-time.c:255
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
strptime
char * strptime(const char *__restrict, const char *__restrict, struct tm *__restrict)
Definition: util-strptime.c:97
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:140
TmThreadsSetThreadTimestamp
void TmThreadsSetThreadTimestamp(const int id, const SCTime_t ts)
Definition: tm-threads.c:2285
SCTimespecAsEpochMillis
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
Definition: util-time.c:631
detect.h
SCTIME_FROM_TIMEVAL
#define SCTIME_FROM_TIMEVAL(tv)
Definition: util-time.h:79
TimeModeIsReady
bool TimeModeIsReady(void)
Definition: util-time.c:92
SCSpinUnlock
#define SCSpinUnlock
Definition: threads-debug.h:238
SCLocalTimeCacheReset
void SCLocalTimeCacheReset(void)
Forget the local times cached by the calling thread, e.g. after the timezone was changed.
Definition: util-time.c:435
util-time.h
SCTIME_TO_TIMEVAL
#define SCTIME_TO_TIMEVAL(tv, t)
Definition: util-time.h:97
TimeModeIsLive
bool TimeModeIsLive(void)
Definition: util-time.c:111
TimeSetIncrementTime
void TimeSetIncrementTime(uint32_t tv_sec)
increment the time in the engine
Definition: util-time.c:180
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:267
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
SCTime_t
Definition: util-time.h:40
TimeSetByThread
void TimeSetByThread(const int thread_id, SCTime_t tv)
Definition: util-time.c:116
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:293
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
MONTHSPERYEAR
#define MONTHSPERYEAR
CreateUtcIsoTimeString
void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:230
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
TimeDifferenceMicros
uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
Definition: util-time.c:636
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
TimeSet
void TimeSet(SCTime_t ts)
Definition: util-time.c:125
suricata-common.h
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
SCTimeRegisterTests
void SCTimeRegisterTests(void)
Definition: util-time.c:673
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
SCSpinInit
#define SCSpinInit
Definition: threads-debug.h:239
str
#define str(s)
Definition: suricata-common.h:313
TimeDeinit
void TimeDeinit(void)
Definition: util-time.c:87
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:235
suricata.h
likely
#define likely(expr)
Definition: util-optimize.h:32
SCStringPatternToTime
int SCStringPatternToTime(char *string, const char **patterns, int num_patterns, struct tm *tp)
Parse a date string based on specified patterns.
Definition: util-time.c:497
SCTIME_ADD_SECS
#define SCTIME_ADD_SECS(ts, s)
Definition: util-time.h:64
TimeInit
void TimeInit(void)
Definition: util-time.c:79
CreateTimeString
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:272
SCTIME_USECS
#define SCTIME_USECS(t)
Definition: util-time.h:56