suricata
util-time.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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  */
23 
24 #ifndef __UTIL_TIME_H__
25 #define __UTIL_TIME_H__
26 
27 /*
28  * The SCTime_t member is broken up as
29  * seconds: 44
30  * useconds: 20
31  *
32  * Over 500000 years can be represented in 44 bits of seconds:
33  * 2^44/(365*24*60*60)
34  * 557855.560
35  * 1048576 microseconds can be represented in 20 bits:
36  * 2^20
37  * 1048576
38  */
39 
40 typedef struct {
41  uint64_t secs : 44;
42  uint64_t usecs : 20;
43 } SCTime_t;
44 
45 #define SCTIME_INIT(t) \
46  { \
47  (t).secs = 0; \
48  (t).usecs = 0; \
49  }
50 #define SCTIME_USECS(t) ((uint64_t)(t).usecs)
51 #define SCTIME_SECS(t) ((uint64_t)(t).secs)
52 #define SCTIME_MSECS(t) (SCTIME_SECS(t) * 1000 + SCTIME_USECS(t) / 1000)
53 #define SCTIME_ADD_SECS(ts, s) SCTIME_FROM_SECS((ts).secs + (s))
54 #define SCTIME_ADD_USECS(ts, us) SCTIME_FROM_USECS((ts).usecs + (us))
55 #define SCTIME_FROM_SECS(s) \
56  (SCTime_t) \
57  { \
58  .secs = (s), .usecs = 0 \
59  }
60 #define SCTIME_FROM_USECS(us) \
61  (SCTime_t) \
62  { \
63  .secs = 0, .usecs = (us) \
64  }
65 #define SCTIME_FROM_TIMEVAL(tv) \
66  (SCTime_t) \
67  { \
68  .secs = (tv)->tv_sec, .usecs = (tv)->tv_usec \
69  }
70 #define SCTIME_FROM_TIMESPEC(ts) \
71  (SCTime_t) \
72  { \
73  .secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec * 1000 \
74  }
75 
76 #define SCTIME_TO_TIMEVAL(tv, t) \
77  (tv)->tv_sec = SCTIME_SECS((t)); \
78  (tv)->tv_usec = SCTIME_USECS((t));
79 #define SCTIME_CMP(a, b, CMP) \
80  ((SCTIME_SECS(a) == SCTIME_SECS(b)) ? (SCTIME_USECS(a) CMP SCTIME_USECS(b)) \
81  : (SCTIME_SECS(a) CMP SCTIME_SECS(b)))
82 #define SCTIME_CMP_GTE(a, b) SCTIME_CMP((a), (b), >=)
83 #define SCTIME_CMP_GT(a, b) SCTIME_CMP((a), (b), >)
84 #define SCTIME_CMP_LT(a, b) SCTIME_CMP((a), (b), <)
85 #define SCTIME_CMP_LTE(a, b) SCTIME_CMP((a), (b), <=)
86 
87 void TimeInit(void);
88 void TimeDeinit(void);
89 
90 void TimeSetByThread(const int thread_id, SCTime_t tv);
91 SCTime_t TimeGet(void);
92 
93 /** \brief initialize a 'struct timespec' from a 'struct timeval'. */
94 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
95 
96 static inline struct timeval TimevalWithSeconds(const struct timeval *ts, const time_t sec_add)
97 {
98 #ifdef timeradd
99  struct timeval add = { .tv_sec = sec_add, .tv_usec = 0 };
100  struct timeval result;
101  timeradd(ts, &add, &result);
102  return result;
103 #else
104  const time_t sec = ts->tv_sec + sec_add;
105  struct timeval result = { .tv_sec = sec, .tv_usec = ts->tv_usec };
106  return result;
107 #endif
108 }
109 
110 /** \brief compare two 'struct timeval' and return if the first is earlier than the second */
111 static inline bool TimevalEarlier(struct timeval *first, struct timeval *second)
112 {
113  /* from man timercmp on Linux: "Some systems (but not Linux/glibc), have a broken timercmp()
114  * implementation, in which CMP of >=, <=, and == do not work; portable applications can instead
115  * use ... !timercmp(..., >) */
116  return !timercmp(first, second, >);
117 }
118 
119 #ifndef timeradd
120 #define timeradd(a, b, r) \
121  do { \
122  (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
123  (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
124  if ((r)->tv_usec >= 1000000) { \
125  (r)->tv_sec++; \
126  (r)->tv_usec -= 1000000; \
127  } \
128  } while (0)
129 #endif
130 
131 #ifdef UNITTESTS
132 void TimeSet(SCTime_t);
133 void TimeSetToCurrentTime(void);
134 void TimeSetIncrementTime(uint32_t);
135 #endif
136 
137 bool TimeModeIsReady(void);
138 void TimeModeSetLive(void);
139 void TimeModeSetOffline (void);
140 bool TimeModeIsLive(void);
141 
142 struct tm *SCLocalTime(time_t timep, struct tm *result);
143 void CreateTimeString(const SCTime_t ts, char *str, size_t size);
144 void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size);
145 void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size);
146 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
147 time_t SCMkTimeUtc(struct tm *tp);
148 int SCStringPatternToTime(char *string, const char **patterns,
149  int num_patterns, struct tm *time);
150 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
151  size_t size);
152 uint64_t SCParseTimeSizeString (const char *str);
153 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
154 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
155 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1);
156 
157 #endif /* __UTIL_TIME_H__ */
158 
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:140
ts
uint64_t ts
Definition: source-erf-file.c:55
TimeSet
void TimeSet(SCTime_t)
Definition: util-time.c:125
CreateFormattedTimeString
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
Definition: util-time.c:246
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
SCMkTimeUtc
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
Definition: util-time.c:442
CreateUtcIsoTimeString
void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:230
SCTimeToStringPattern
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
Definition: util-time.c:541
TimeInit
void TimeInit(void)
Definition: util-time.c:79
SCTime_t::secs
uint64_t secs
Definition: util-time.h:41
TimeModeIsLive
bool TimeModeIsLive(void)
Definition: util-time.c:111
SCTime_t::usecs
uint64_t usecs
Definition: util-time.h:42
SCTimespecAsEpochMillis
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
Definition: util-time.c:639
SCTime_t
Definition: util-time.h:40
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
TimeModeIsReady
bool TimeModeIsReady(void)
Definition: util-time.c:92
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
CreateIsoTimeString
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:209
TimeDeinit
void TimeDeinit(void)
Definition: util-time.c:87
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
str
#define str(s)
Definition: suricata-common.h:280
TimeDifferenceMicros
uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
Definition: util-time.c:644
SCParseTimeSizeString
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
Definition: util-time.c:570
timeradd
#define timeradd(a, b, r)
Definition: util-time.h:120
TimeSetByThread
void TimeSetByThread(const int thread_id, SCTime_t tv)
Definition: util-time.c:116
SCGetSecondsUntil
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
Definition: util-time.c:621
CreateTimeString
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:272
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:267
TimeSetIncrementTime
void TimeSetIncrementTime(uint32_t)
increment the time in the engine
Definition: util-time.c:180
SCStringPatternToTime
int SCStringPatternToTime(char *string, const char **patterns, int num_patterns, struct tm *time)
Parse a date string based on specified patterns.
Definition: util-time.c:485