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