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