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 
51 #define SCTIME_INITIALIZER \
52  (SCTime_t) \
53  { \
54  .secs = 0, .usecs = 0 \
55  }
56 #define SCTIME_USECS(t) ((uint64_t)(t).usecs)
57 #define SCTIME_SECS(t) ((uint64_t)(t).secs)
58 #define SCTIME_MSECS(t) (SCTIME_SECS(t) * 1000 + SCTIME_USECS(t) / 1000)
59 #define SCTIME_ADD_USECS(ts, us) \
60  (SCTime_t) \
61  { \
62  .secs = (ts).secs + ((ts).usecs + (us)) / 1000000, .usecs = ((ts).usecs + (us)) % 1000000 \
63  }
64 #define SCTIME_ADD_SECS(ts, s) \
65  (SCTime_t) \
66  { \
67  .secs = (ts).secs + (s), .usecs = (ts).usecs \
68  }
69 #define SCTIME_FROM_SECS(s) \
70  (SCTime_t) \
71  { \
72  .secs = (s), .usecs = 0 \
73  }
74 #define SCTIME_FROM_USECS(us) \
75  (SCTime_t) \
76  { \
77  .secs = 0, .usecs = (us) \
78  }
79 #define SCTIME_FROM_TIMEVAL(tv) \
80  (SCTime_t) \
81  { \
82  .secs = (tv)->tv_sec, .usecs = (tv)->tv_usec \
83  }
84 /** \brief variant to deal with potentially bad timestamps, like from pcap files */
85 #define SCTIME_FROM_TIMEVAL_UNTRUSTED(tv) \
86  (SCTime_t) \
87  { \
88  .secs = ((tv)->tv_sec > 0) ? (tv)->tv_sec : 0, \
89  .usecs = ((tv)->tv_usec > 0) ? (tv)->tv_usec : 0 \
90  }
91 #define SCTIME_FROM_TIMESPEC(ts) \
92  (SCTime_t) \
93  { \
94  .secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec / 1000 \
95  }
96 
97 #define SCTIME_TO_TIMEVAL(tv, t) \
98  (tv)->tv_sec = SCTIME_SECS((t)); \
99  (tv)->tv_usec = SCTIME_USECS((t));
100 #define SCTIME_CMP(a, b, CMP) \
101  ((SCTIME_SECS(a) == SCTIME_SECS(b)) ? (SCTIME_USECS(a) CMP SCTIME_USECS(b)) \
102  : (SCTIME_SECS(a) CMP SCTIME_SECS(b)))
103 #define SCTIME_CMP_GTE(a, b) SCTIME_CMP((a), (b), >=)
104 #define SCTIME_CMP_GT(a, b) SCTIME_CMP((a), (b), >)
105 #define SCTIME_CMP_LT(a, b) SCTIME_CMP((a), (b), <)
106 #define SCTIME_CMP_LTE(a, b) SCTIME_CMP((a), (b), <=)
107 #define SCTIME_CMP_NEQ(a, b) SCTIME_CMP((a), (b), !=)
108 
109 void TimeInit(void);
110 void TimeDeinit(void);
111 
112 void TimeSetByThread(const int thread_id, SCTime_t tv);
113 SCTime_t TimeGet(void);
114 
115 /** \brief initialize a 'struct timespec' from a 'struct timeval'. */
116 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
117 
118 /** \brief compare two 'struct timeval' and return if the first is earlier than the second */
119 static inline bool TimevalEarlier(struct timeval *first, struct timeval *second)
120 {
121  /* from man timercmp on Linux: "Some systems (but not Linux/glibc), have a broken timercmp()
122  * implementation, in which CMP of >=, <=, and == do not work; portable applications can instead
123  * use ... !timercmp(..., >) */
124  return !timercmp(first, second, >);
125 }
126 
127 #ifndef timeradd
128 #define timeradd(a, b, r) \
129  do { \
130  (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
131  (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
132  if ((r)->tv_usec >= 1000000) { \
133  (r)->tv_sec++; \
134  (r)->tv_usec -= 1000000; \
135  } \
136  } while (0)
137 #endif
138 
139 #ifdef UNITTESTS
140 void TimeSet(SCTime_t);
141 void TimeSetToCurrentTime(void);
142 void TimeSetIncrementTime(uint32_t);
143 #endif
144 
145 bool TimeModeIsReady(void);
146 void TimeModeSetLive(void);
147 void TimeModeSetOffline (void);
148 bool TimeModeIsLive(void);
149 
150 struct tm *SCLocalTime(time_t timep, struct tm *result);
151 void CreateTimeString(const SCTime_t ts, char *str, size_t size);
152 void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size);
153 void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size);
154 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
155 time_t SCMkTimeUtc(struct tm *tp);
156 int SCStringPatternToTime(char *string, const char **patterns,
157  int num_patterns, struct tm *time);
158 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
159  size_t size);
160 uint64_t SCParseTimeSizeString (const char *str);
161 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
162 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
163 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1);
164 
165 #endif /* __UTIL_TIME_H__ */
166 
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:291
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
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