suricata
suricata-common.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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  * Common includes, etc.
24  */
25 
26 #ifndef SURICATA_SURICATA_COMMON_H
27 #define SURICATA_SURICATA_COMMON_H
28 
29 #ifdef DEBUG
30 #define DBG_PERF
31 #endif
32 
33 #define _GNU_SOURCE
34 #define __USE_GNU
35 
36 #if defined(__clang_analyzer__)
37 /* clang analyzer acts as DEBUG_VALIDATION in some places, so
38  * force this so #ifdef DEBUG_VALIDATION code gets included */
39 #define DEBUG_VALIDATION 1
40 #endif
41 
42 #if defined(__has_feature)
43 #if __has_feature(address_sanitizer)
44 #define SC_ADDRESS_SANITIZER 1
45 #endif
46 #elif defined(__SANITIZE_ADDRESS__)
47 #define SC_ADDRESS_SANITIZER 1
48 #endif
49 
50 #include "autoconf.h"
51 
52 #ifndef REVISION
53 #define REVISION "undefined"
54 #endif
55 #ifndef __SCFILENAME__
56 #define __SCFILENAME__ "undefined"
57 #endif
58 
59 #ifndef CLS
60 #warning "L1 cache line size not detected during build. Assuming 64 bytes."
61 #define CLS 64
62 #endif
63 
64 #if HAVE_DIRENT_H
65 #include <dirent.h>
66 #endif
67 
68 #if HAVE_STDIO_H
69 #include <stdio.h>
70 #endif
71 
72 #if HAVE_STDDEF_H
73 #include <stddef.h>
74 #endif
75 
76 #if HAVE_STDINT_h
77 #include <stdint.h>
78 #endif
79 
80 #if HAVE_STDBOOL_H
81 #include <stdbool.h>
82 #endif
83 
84 #if HAVE_STDARG_H
85 #include <stdarg.h>
86 #endif
87 
88 #ifdef HAVE_STDLIB_H
89 #include <stdlib.h>
90 #endif
91 
92 #if HAVE_ERRNO_H
93 #include <errno.h>
94 #endif
95 
96 #if HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99 
100 #if HAVE_INTTYPES_H
101 #include <inttypes.h>
102 #endif
103 
104 #if HAVE_LIMITS_H
105 #include <limits.h>
106 #endif
107 
108 #if HAVE_CTYPE_H
109 #include <ctype.h>
110 #endif
111 
112 #if HAVE_STRING_H
113 #include <string.h>
114 #endif
115 
116 #if HAVE_STRINGS_H
117 #include <strings.h>
118 #endif
119 
120 #if HAVE_FCNTL_H
121 #include <fcntl.h>
122 #endif
123 
124 #ifdef HAVE_TIME_H
125 #include <time.h>
126 #endif
127 
128 #if HAVE_SYS_SYSCALL_H
129 #include <sys/syscall.h>
130 #endif
131 
132 #if HAVE_SYSCALL_H
133 #include <syscall.h>
134 #endif
135 
136 #if HAVE_SYS_TYPES_H
137 #include <sys/types.h> /* for gettid(2) */
138 #endif
139 
140 #if HAVE_SCHED_H
141 #include <sched.h> /* for sched_setaffinity(2) */
142 #endif
143 
144 #ifdef HAVE_TYPE_U_LONG_NOT_DEFINED
145 typedef unsigned long int u_long;
146 #endif
147 #ifdef HAVE_TYPE_U_INT_NOT_DEFINED
148 typedef unsigned int u_int;
149 #endif
150 #ifdef HAVE_TYPE_U_SHORT_NOT_DEFINED
151 typedef unsigned short u_short;
152 #endif
153 #ifdef HAVE_TYPE_U_CHAR_NOT_DEFINED
154 typedef unsigned char u_char;
155 #endif
156 
157 #include <pcre2.h>
158 
159 #ifdef HAVE_SYSLOG_H
160 #include <syslog.h>
161 #else
162 #ifdef OS_WIN32
163 #include "win32-syslog.h"
164 #endif /* OS_WIN32 */
165 #endif /* HAVE_SYSLOG_H */
166 
167 #ifdef OS_WIN32
168 #include "win32-misc.h"
169 #include "win32-service.h"
170 #endif /* OS_WIN32 */
171 
172 #if HAVE_SYS_TIME_H
173 #include <sys/time.h>
174 #endif
175 
176 #if HAVE_POLL_H
177 #include <poll.h>
178 #endif
179 
180 #if HAVE_SYS_SIGNAL_H
181 #include <sys/signal.h>
182 #endif
183 
184 #if HAVE_SIGNAL_H
185 #include <signal.h>
186 #endif
187 
188 #if HAVE_SYS_SOCKET_H
189 #include <sys/socket.h>
190 #endif
191 
192 #if HAVE_SYS_STAT_H
193 #include <sys/stat.h>
194 #endif
195 
196 #if HAVE_SYS_IOCTL_H
197 #include <sys/ioctl.h>
198 #endif
199 
200 #if HAVE_SYS_MMAN_H
201 #include <sys/mman.h>
202 #endif
203 
204 #if HAVE_SYS_RANDOM_H
205 #include <sys/random.h>
206 #endif
207 
208 #if HAVE_NETINET_IN_H
209 #include <netinet/in.h>
210 #endif
211 
212 #if HAVE_ARPA_INET_H
213 #include <arpa/inet.h>
214 #endif
215 
216 #if HAVE_NETDB_H
217 #include <netdb.h>
218 #endif
219 
220 #if HAVE_MALLOC_H
221 #include <malloc.h>
222 #endif
223 
224 #if __CYGWIN__
225 #if !defined _X86_ && !defined __x86_64
226 #define _X86_
227 #endif
228 #endif
229 
230 #if !__CYGWIN__
231 #ifdef HAVE_WINSOCK2_H
232 #include <winsock2.h>
233 #endif
234 #ifdef HAVE_WS2TCPIP_H
235 #include <ws2tcpip.h>
236 #endif
237 #endif /* !__CYGWIN__ */
238 
239 #ifdef HAVE_WINDOWS_H
240 #ifndef _WIN32_WINNT
241 #define _WIN32_WINNT 0x0501
242 #endif
243 #include <windows.h>
244 #endif
245 
246 #ifdef HAVE_W32API_WINBASE_H
247 #include <w32api/winbase.h>
248 #endif
249 
250 #ifdef HAVE_W32API_WTYPES_H
251 #include <w32api/wtypes.h>
252 #endif
253 
254 #ifndef SC_PCAP_DONT_INCLUDE_PCAP_H
255 #ifdef HAVE_PCAP_H
256 #include <pcap.h>
257 #endif
258 
259 #ifdef HAVE_PCAP_PCAP_H
260 #include <pcap/pcap.h>
261 #endif
262 #endif
263 
264 #ifdef HAVE_UTIME_H
265 #include <utime.h>
266 #endif
267 
268 #ifdef HAVE_LIBGEN_H
269 #include <libgen.h>
270 #endif
271 
272 #ifdef HAVE_GRP_H
273 #include <grp.h>
274 #endif
275 
276 #ifdef HAVE_PWD_H
277 #include <pwd.h>
278 #endif
279 
280 #include <jansson.h>
281 #ifndef JSON_ESCAPE_SLASH
282 #define JSON_ESCAPE_SLASH 0
283 #endif
284 
285 #ifdef HAVE_MAGIC
286 #include <magic.h>
287 #endif
288 
289 #ifdef HAVE_MATH_H
290 #include <math.h>
291 #endif
292 
293 #ifdef HAVE_MM_MALLOC_H
294 #include <mm_malloc.h>
295 #endif
296 
297 /* we need this to stringify the defines which are supplied at compiletime see:
298  http://gcc.gnu.org/onlinedocs/gcc-3.4.1/cpp/Stringification.html#Stringification */
299 #define xstr(s) str(s)
300 #define str(s) #s
301 
302 #if CPPCHECK==1
303  #define BUG_ON(x) if (((x))) exit(1)
304 #else
305  #if defined HAVE_ASSERT_H && !defined NDEBUG
306  #include <assert.h>
307  #define BUG_ON(x) assert(!(x))
308  #else
309  #define BUG_ON(x) do { \
310  if (((x))) { \
311  fprintf(stderr, "BUG at %s:%d(%s)\n", __FILE__, __LINE__, __func__); \
312  fprintf(stderr, "Code: '%s'\n", xstr((x))); \
313  exit(EXIT_FAILURE); \
314  } \
315  } while(0)
316  #endif
317 #endif
318 
319 /** type for the internal signature id. Since it's used in the matching engine
320  * extensively keeping this as small as possible reduces the overall memory
321  * footprint of the engine. Set to uint32_t if the engine needs to support
322  * more than 64k sigs. */
323 //#define SigIntId uint16_t
324 #define SigIntId uint32_t
325 
326 /** same for pattern id's */
327 #define PatIntId uint32_t
328 
329 /** FreeBSD does not define __WORDSIZE, but it uses __LONG_BIT */
330 #ifndef __WORDSIZE
331  #ifdef __LONG_BIT
332  #define __WORDSIZE __LONG_BIT
333  #else
334  #ifdef LONG_BIT
335  #define __WORDSIZE LONG_BIT
336  #endif
337  #endif
338 #endif
339 
340 /** Windows does not define __WORDSIZE, but it uses __X86__ */
341 #ifndef __WORDSIZE
342  #if defined(__X86__) || defined(_X86_) || defined(_M_IX86)
343  #define __WORDSIZE 32
344  #else
345  #if defined(__X86_64__) || defined(_X86_64_) || \
346  defined(__x86_64) || defined(__x86_64__) || \
347  defined(__amd64) || defined(__amd64__)
348  #define __WORDSIZE 64
349  #endif
350  #endif
351 #endif
352 
353 /** if not succesful yet try the data models */
354 #ifndef __WORDSIZE
355  #if defined(_ILP32) || defined(__ILP32__)
356  #define __WORDSIZE 32
357  #endif
358  #if defined(_LP64) || defined(__LP64__)
359  #define __WORDSIZE 64
360  #endif
361 #endif
362 
363 #ifndef __WORDSIZE
364  #warning Defaulting to __WORDSIZE 32
365  #define __WORDSIZE 32
366 #endif
367 
368 /** darwin doesn't defined __BYTE_ORDER and friends, but BYTE_ORDER */
369 #ifndef __BYTE_ORDER
370  #if defined(BYTE_ORDER)
371  #define __BYTE_ORDER BYTE_ORDER
372  #elif defined(__BYTE_ORDER__)
373  #define __BYTE_ORDER __BYTE_ORDER__
374  #else
375  #error "byte order not detected"
376  #endif
377 #endif
378 
379 #ifndef __LITTLE_ENDIAN
380  #if defined(LITTLE_ENDIAN)
381  #define __LITTLE_ENDIAN LITTLE_ENDIAN
382  #elif defined(__ORDER_LITTLE_ENDIAN__)
383  #define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
384  #endif
385 #endif
386 
387 #ifndef __BIG_ENDIAN
388  #if defined(BIG_ENDIAN)
389  #define __BIG_ENDIAN BIG_ENDIAN
390  #elif defined(__ORDER_BIG_ENDIAN__)
391  #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
392  #endif
393 #endif
394 
395 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
396  #error "byte order: can't figure out big or little"
397 #endif
398 
399 #ifndef MIN
400 #define MIN(x, y) (((x)<(y))?(x):(y))
401 #endif
402 
403 #ifndef MAX
404 #define MAX(x, y) (((x)<(y))?(y):(x))
405 #endif
406 
407 #define BIT_U8(n) ((uint8_t)(1 << (n)))
408 #define BIT_U16(n) ((uint16_t)(1 << (n)))
409 #define BIT_U32(n) ((uint32_t)(1UL << (n)))
410 #define BIT_U64(n) (1ULL << (n))
411 
412 #define WARN_UNUSED __attribute__((warn_unused_result))
413 
414 #if defined(__MINGW32__)
415 #define ATTR_FMT_PRINTF(x, y) __attribute__((format(__MINGW_PRINTF_FORMAT, (x), (y))))
416 #elif defined(__GNUC__)
417 #define ATTR_FMT_PRINTF(x, y) __attribute__((format(printf, (x), (y))))
418 #else
419 #define ATTR_FMT_PRINTF(x, y)
420 #endif
421 
422 #define SCNtohl(x) (uint32_t)ntohl((x))
423 #define SCNtohs(x) (uint16_t)ntohs((x))
424 
425 /* swap flags if one of them is set, otherwise do nothing. */
426 #define SWAP_FLAGS(flags, a, b) \
427  do { \
428  if (((flags) & ((a)|(b))) == (a)) { \
429  (flags) &= ~(a); \
430  (flags) |= (b); \
431  } else if (((flags) & ((a)|(b))) == (b)) { \
432  (flags) &= ~(b); \
433  (flags) |= (a); \
434  } \
435  } while(0)
436 
437 #define SWAP_VARS(type, a, b) \
438  do { \
439  type t = (a); \
440  (a) = (b); \
441  (b) = t; \
442  } while (0)
443 
444 #include <ctype.h>
445 #define u8_tolower(c) ((uint8_t)tolower((uint8_t)(c)))
446 #define u8_toupper(c) ((uint8_t)toupper((uint8_t)(c)))
447 
464 
467 
468 /** \note update PacketProfileLoggerIdToString if you change anything here */
469 typedef enum {
471 
472  /* TX loggers first for low logger IDs */
480 
481  /** \warning Note that transaction loggers here with a value > 31
482  will not work. */
483 
484  /* non-tx loggers below */
485 
504 
505  /* An ID that can be used by loggers registered by plugins and/or
506  * library users. */
508 
509  /* Must come last. */
511 } LoggerId;
512 
513 /* If we don't have Lua, create a typedef for lua_State so the
514  * exported Lua functions don't fail the build. */
515 typedef struct lua_State lua_State;
516 
517 #include "tm-threads-common.h"
518 #include "util-optimize.h"
519 #include "util-time.h"
520 #include "util-mem.h"
521 #include "util-memcmp.h"
522 #include "util-atomic.h"
523 #include "util-unittest.h"
524 
525 // pseudo system headers
526 #include "queue.h"
527 #include "tree.h"
528 
529 #ifndef HAVE_STRLCAT
530 size_t strlcat(char *, const char *src, size_t siz);
531 #endif
532 #ifndef HAVE_STRLCPY
533 size_t strlcpy(char *dst, const char *src, size_t siz);
534 #endif
535 #ifndef HAVE_STRPTIME
536 char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict);
537 #endif
538 
539 #ifndef HAVE_FWRITE_UNLOCKED
540 #define SCFwriteUnlocked fwrite
541 #define SCFflushUnlocked fflush
542 #define SCClearErrUnlocked clearerr
543 #define SCFerrorUnlocked ferror
544 #else
545 #define SCFwriteUnlocked fwrite_unlocked
546 #define SCFflushUnlocked fflush_unlocked
547 #define SCClearErrUnlocked clearerr_unlocked
548 #define SCFerrorUnlocked ferror_unlocked
549 #endif
550 extern int coverage_unittests;
551 extern int g_ut_modules;
552 extern int g_ut_covered;
553 
554 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
555 
556 #endif /* SURICATA_SURICATA_COMMON_H */
win32-misc.h
PROF_DETECT_GETSGH
@ PROF_DETECT_GETSGH
Definition: suricata-common.h:450
PacketProfileDetectId_
PacketProfileDetectId_
Definition: suricata-common.h:448
g_ut_modules
int g_ut_modules
Definition: suricata.c:893
LOGGER_USER
@ LOGGER_USER
Definition: suricata-common.h:507
PROF_DETECT_PF_PAYLOAD
@ PROF_DETECT_PF_PAYLOAD
Definition: suricata-common.h:455
LOGGER_ALERT_SYSLOG
@ LOGGER_ALERT_SYSLOG
Definition: suricata-common.h:488
LOGGER_FILEDATA
@ LOGGER_FILEDATA
Definition: suricata-common.h:479
LOGGER_JSON_STATS
@ LOGGER_JSON_STATS
Definition: suricata-common.h:498
LOGGER_JSON_ALERT
@ LOGGER_JSON_ALERT
Definition: suricata-common.h:489
LOGGER_JSON_ARP
@ LOGGER_JSON_ARP
Definition: suricata-common.h:503
PROF_DETECT_ALERT
@ PROF_DETECT_ALERT
Definition: suricata-common.h:461
LoggerId
LoggerId
Definition: suricata-common.h:469
PROF_DETECT_SIZE
@ PROF_DETECT_SIZE
Definition: suricata-common.h:465
g_ut_covered
int g_ut_covered
Definition: suricata.c:894
PROF_DETECT_PF_TX
@ PROF_DETECT_PF_TX
Definition: suricata-common.h:456
PROF_DETECT_CLEANUP
@ PROF_DETECT_CLEANUP
Definition: suricata-common.h:463
tm-threads-common.h
LOGGER_STATS
@ LOGGER_STATS
Definition: suricata-common.h:497
util-unittest.h
PROF_DETECT_PF_SORT2
@ PROF_DETECT_PF_SORT2
Definition: suricata-common.h:459
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
PROF_DETECT_PF_SORT1
@ PROF_DETECT_PF_SORT1
Definition: suricata-common.h:458
lua_State
struct lua_State lua_State
Definition: suricata-common.h:515
util-memcmp.h
PROF_DETECT_TX_UPDATE
@ PROF_DETECT_TX_UPDATE
Definition: suricata-common.h:462
LOGGER_JSON_FLOW
@ LOGGER_JSON_FLOW
Definition: suricata-common.h:495
LOGGER_HTTP
@ LOGGER_HTTP
Definition: suricata-common.h:473
LOGGER_TCP_DATA
@ LOGGER_TCP_DATA
Definition: suricata-common.h:494
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
LOGGER_JSON_FILE
@ LOGGER_JSON_FILE
Definition: suricata-common.h:493
PacketProfileDetectId
enum PacketProfileDetectId_ PacketProfileDetectId
PROF_DETECT_PF_PKT
@ PROF_DETECT_PF_PKT
Definition: suricata-common.h:454
LOGGER_UNDEFINED
@ LOGGER_UNDEFINED
Definition: suricata-common.h:470
util-atomic.h
util-time.h
LOGGER_TLS
@ LOGGER_TLS
Definition: suricata-common.h:476
LOGGER_SIZE
@ LOGGER_SIZE
Definition: suricata-common.h:510
LOGGER_JSON_NETFLOW
@ LOGGER_JSON_NETFLOW
Definition: suricata-common.h:496
PROF_DETECT_IPONLY
@ PROF_DETECT_IPONLY
Definition: suricata-common.h:451
queue.h
win32-syslog.h
tree.h
util-mem.h
LOGGER_PCAP
@ LOGGER_PCAP
Definition: suricata-common.h:499
LOGGER_JSON_METADATA
@ LOGGER_JSON_METADATA
Definition: suricata-common.h:500
PROF_DETECT_PF_RECORD
@ PROF_DETECT_PF_RECORD
Definition: suricata-common.h:457
LOGGER_JSON_DROP
@ LOGGER_JSON_DROP
Definition: suricata-common.h:491
PROF_DETECT_SETUP
@ PROF_DETECT_SETUP
Definition: suricata-common.h:449
LOGGER_ALERT_DEBUG
@ LOGGER_ALERT_DEBUG
Definition: suricata-common.h:486
util-optimize.h
win32-service.h
LOGGER_FILE
@ LOGGER_FILE
Definition: suricata-common.h:478
PROF_DETECT_RULES
@ PROF_DETECT_RULES
Definition: suricata-common.h:452
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:477
PROF_DETECT_NONMPMLIST
@ PROF_DETECT_NONMPMLIST
Definition: suricata-common.h:460
src
uint16_t src
Definition: app-layer-dnp3.h:5
LOGGER_FILE_STORE
@ LOGGER_FILE_STORE
Definition: suricata-common.h:492
LOGGER_TLS_STORE_CLIENT
@ LOGGER_TLS_STORE_CLIENT
Definition: suricata-common.h:475
LOGGER_JSON_FRAME
@ LOGGER_JSON_FRAME
Definition: suricata-common.h:501
PROF_DETECT_TX
@ PROF_DETECT_TX
Definition: suricata-common.h:453
LOGGER_TLS_STORE
@ LOGGER_TLS_STORE
Definition: suricata-common.h:474
coverage_unittests
int coverage_unittests
Definition: suricata.c:892
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
LOGGER_JSON_STREAM
@ LOGGER_JSON_STREAM
Definition: suricata-common.h:502
LOGGER_ALERT_FAST
@ LOGGER_ALERT_FAST
Definition: suricata-common.h:487
LOGGER_JSON_ANOMALY
@ LOGGER_JSON_ANOMALY
Definition: suricata-common.h:490