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 SURICATA_UTIL_TIME_H
25 #define SURICATA_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) ((t).usecs)
57 #define SCTIME_SECS(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 #define SCTIME_CMP_EQ(a, b) SCTIME_CMP((a), (b), ==)
109 
110 static inline SCTime_t SCTimeGetTime(void)
111 {
112  struct timeval tv;
113  gettimeofday(&tv, NULL);
114  return SCTIME_FROM_TIMEVAL(&tv);
115 }
116 
117 void TimeInit(void);
118 void TimeDeinit(void);
119 
120 void TimeSetByThread(const int thread_id, SCTime_t tv);
121 SCTime_t TimeGet(void);
122 
123 /** \brief initialize a 'struct timespec' from a 'struct timeval'. */
124 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
125 
126 #ifndef timeradd
127 #define timeradd(a, b, r) \
128  do { \
129  (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
130  (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
131  if ((r)->tv_usec >= 1000000) { \
132  (r)->tv_sec++; \
133  (r)->tv_usec -= 1000000; \
134  } \
135  } while (0)
136 #endif
137 
138 #ifdef UNITTESTS
139 void TimeSet(SCTime_t);
140 void TimeSetToCurrentTime(void);
141 void TimeSetIncrementTime(uint32_t);
142 #endif
143 
144 bool TimeModeIsReady(void);
145 void TimeModeSetLive(void);
146 void TimeModeSetOffline (void);
147 bool TimeModeIsLive(void);
148 
149 struct tm *SCLocalTime(time_t timep, struct tm *result);
150 void CreateTimeString(const SCTime_t ts, char *str, size_t size);
151 void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size);
152 void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size);
153 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
154 time_t SCMkTimeUtc(struct tm *tp);
155 int SCStringPatternToTime(char *string, const char **patterns,
156  int num_patterns, struct tm *time);
157 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
158  size_t size);
159 uint64_t SCParseTimeSizeString (const char *str);
160 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
161 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
162 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1);
163 
164 #endif /* SURICATA_UTIL_TIME_H */
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
SCTIME_FROM_TIMEVAL
#define SCTIME_FROM_TIMEVAL(tv)
Definition: util-time.h:79
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