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