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