suricata
suricata-common.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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_COMMON_H__
27 #define __SURICATA_COMMON_H__
28 
29 #ifdef DEBUG
30 #define DBG_PERF
31 #endif
32 
33 #define TRUE 1
34 #define FALSE 0
35 
36 #define _GNU_SOURCE
37 #define __USE_GNU
38 
39 #if HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42 
43 #ifndef CLS
44 #warning "L1 cache line size not detected during build. Assuming 64 bytes."
45 #define CLS 64
46 #endif
47 
48 #if HAVE_DIRENT_H
49 #include <dirent.h>
50 #endif
51 
52 #if HAVE_STDIO_H
53 #include <stdio.h>
54 #endif
55 
56 #if HAVE_STDDEF_H
57 #include <stddef.h>
58 #endif
59 
60 #if HAVE_STDINT_h
61 #include <stdint.h>
62 #endif
63 
64 #if HAVE_STDBOOL_H
65 #include <stdbool.h>
66 #endif
67 
68 #if HAVE_STDARG_H
69 #include <stdarg.h>
70 #endif
71 
72 #ifdef HAVE_STDLIB_H
73 #include <stdlib.h>
74 #endif
75 
76 #if HAVE_ERRNO_H
77 #include <errno.h>
78 #endif
79 
80 #if HAVE_UNISTD_H
81 #include <unistd.h>
82 #endif
83 
84 #if HAVE_INTTYPES_H
85 #include <inttypes.h>
86 #endif
87 
88 #if HAVE_LIMITS_H
89 #include <limits.h>
90 #endif
91 
92 #if HAVE_CTYPE_H
93 #include <ctype.h>
94 #endif
95 
96 #if HAVE_STRING_H
97 #include <string.h>
98 #endif
99 
100 #if HAVE_STRINGS_H
101 #include <strings.h>
102 #endif
103 
104 #if HAVE_FCNTL_H
105 #include <fcntl.h>
106 #endif
107 
108 #ifdef HAVE_TIME_H
109 #include <time.h>
110 #endif
111 
112 #if HAVE_SYS_SYSCALL_H
113 #include <sys/syscall.h>
114 #endif
115 
116 #if HAVE_SYSCALL_H
117 #include <syscall.h>
118 #endif
119 
120 #if HAVE_SYS_TYPES_H
121 #include <sys/types.h> /* for gettid(2) */
122 #endif
123 
124 #if HAVE_SCHED_H
125 #include <sched.h> /* for sched_setaffinity(2) */
126 #endif
127 
128 #include <pcre.h>
129 
130 #ifdef HAVE_SYSLOG_H
131 #include <syslog.h>
132 #else
133 #ifdef OS_WIN32
134 #include "win32-syslog.h"
135 #endif /* OS_WIN32 */
136 #endif /* HAVE_SYSLOG_H */
137 
138 #ifdef OS_WIN32
139 #include "win32-misc.h"
140 #include "win32-service.h"
141 #endif /* OS_WIN32 */
142 
143 #if HAVE_SYS_TIME_H
144 #include <sys/time.h>
145 #endif
146 
147 #if HAVE_POLL_H
148 #include <poll.h>
149 #endif
150 
151 #if HAVE_SYS_SIGNAL_H
152 #include <sys/signal.h>
153 #endif
154 
155 #if HAVE_SIGNAL_H
156 #include <signal.h>
157 #endif
158 
159 #if HAVE_SYS_TYPES_H
160 #include <sys/types.h>
161 #endif
162 
163 #if HAVE_SYS_SOCKET_H
164 #include <sys/socket.h>
165 #endif
166 
167 #if HAVE_SYS_STAT_H
168 #include <sys/stat.h>
169 #endif
170 
171 #if HAVE_SYS_MMAN_H
172 #include <sys/mman.h>
173 #endif
174 
175 #if HAVE_SYS_RANDOM_H
176 #include <sys/random.h>
177 #endif
178 
179 #if HAVE_NETINET_IN_H
180 #include <netinet/in.h>
181 #endif
182 
183 #if HAVE_ARPA_INET_H
184 #include <arpa/inet.h>
185 #endif
186 
187 #if HAVE_NETDB_H
188 #include <netdb.h>
189 #endif
190 
191 #ifndef SC_PCAP_DONT_INCLUDE_PCAP_H
192 #ifdef HAVE_PCAP_H
193 #include <pcap.h>
194 #endif
195 
196 #ifdef HAVE_PCAP_PCAP_H
197 #include <pcap/pcap.h>
198 #endif
199 #endif
200 
201 #ifdef HAVE_UTIME_H
202 #include <utime.h>
203 #endif
204 
205 #ifdef HAVE_LIBGEN_H
206 #include <libgen.h>
207 #endif
208 
209 #if __CYGWIN__
210 #if !defined _X86_ && !defined __x86_64
211 #define _X86_
212 #endif
213 #endif
214 
215 #if !__CYGWIN__
216 #ifdef HAVE_WINSOCK2_H
217 #include <winsock2.h>
218 #endif
219 #ifdef HAVE_WS2TCPIP_H
220 #include <ws2tcpip.h>
221 #endif
222 #endif /* !__CYGWIN__ */
223 
224 #ifdef HAVE_WINDOWS_H
225 #ifndef _WIN32_WINNT
226 #define _WIN32_WINNT 0x0501
227 #endif
228 #include <windows.h>
229 #endif
230 
231 #ifdef HAVE_W32API_WINBASE_H
232 #include <w32api/winbase.h>
233 #endif
234 
235 #ifdef HAVE_W32API_WTYPES_H
236 #include <w32api/wtypes.h>
237 #endif
238 
239 #include <jansson.h>
240 #ifndef JSON_ESCAPE_SLASH
241 #define JSON_ESCAPE_SLASH 0
242 #endif
243 /* Appears not all current distros have jansson that defines this. */
244 #ifndef json_boolean
245 #define json_boolean(val) SCJsonBool((val))
246 //#define json_boolean(val) ((val) ? json_true() : json_false())
247 #endif
248 
249 #ifdef HAVE_MAGIC
250 #include <magic.h>
251 #endif
252 
253 /* we need this to stringify the defines which are supplied at compiletime see:
254  http://gcc.gnu.org/onlinedocs/gcc-3.4.1/cpp/Stringification.html#Stringification */
255 #define xstr(s) str(s)
256 #define str(s) #s
257 
258 #if CPPCHECK==1
259  #define BUG_ON(x) if (((x))) exit(1)
260 #else
261  #if defined HAVE_ASSERT_H && !defined NDEBUG
262  #include <assert.h>
263  #define BUG_ON(x) assert(!(x))
264  #else
265  #define BUG_ON(x) do { \
266  if (((x))) { \
267  fprintf(stderr, "BUG at %s:%d(%s)\n", __FILE__, __LINE__, __func__); \
268  fprintf(stderr, "Code: '%s'\n", xstr((x))); \
269  exit(EXIT_FAILURE); \
270  } \
271  } while(0)
272  #endif
273 #endif
274 
275 /** type for the internal signature id. Since it's used in the matching engine
276  * extensively keeping this as small as possible reduces the overall memory
277  * footprint of the engine. Set to uint32_t if the engine needs to support
278  * more than 64k sigs. */
279 //#define SigIntId uint16_t
280 #define SigIntId uint32_t
281 
282 /** same for pattern id's */
283 #define PatIntId uint32_t
284 
285 /** FreeBSD does not define __WORDSIZE, but it uses __LONG_BIT */
286 #ifndef __WORDSIZE
287  #ifdef __LONG_BIT
288  #define __WORDSIZE __LONG_BIT
289  #else
290  #ifdef LONG_BIT
291  #define __WORDSIZE LONG_BIT
292  #endif
293  #endif
294 #endif
295 
296 /** Windows does not define __WORDSIZE, but it uses __X86__ */
297 #ifndef __WORDSIZE
298  #if defined(__X86__) || defined(_X86_) || defined(_M_IX86)
299  #define __WORDSIZE 32
300  #else
301  #if defined(__X86_64__) || defined(_X86_64_) || \
302  defined(__x86_64) || defined(__x86_64__) || \
303  defined(__amd64) || defined(__amd64__)
304  #define __WORDSIZE 64
305  #endif
306  #endif
307 #endif
308 
309 /** if not succesful yet try the data models */
310 #ifndef __WORDSIZE
311  #if defined(_ILP32) || defined(__ILP32__)
312  #define __WORDSIZE 32
313  #endif
314  #if defined(_LP64) || defined(__LP64__)
315  #define __WORDSIZE 64
316  #endif
317 #endif
318 
319 #ifndef __WORDSIZE
320  #warning Defaulting to __WORDSIZE 32
321  #define __WORDSIZE 32
322 #endif
323 
324 /** darwin doesn't defined __BYTE_ORDER and friends, but BYTE_ORDER */
325 #ifndef __BYTE_ORDER
326  #if defined(BYTE_ORDER)
327  #define __BYTE_ORDER BYTE_ORDER
328  #elif defined(__BYTE_ORDER__)
329  #define __BYTE_ORDER __BYTE_ORDER__
330  #else
331  #error "byte order not detected"
332  #endif
333 #endif
334 
335 #ifndef __LITTLE_ENDIAN
336  #if defined(LITTLE_ENDIAN)
337  #define __LITTLE_ENDIAN LITTLE_ENDIAN
338  #elif defined(__ORDER_LITTLE_ENDIAN__)
339  #define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
340  #endif
341 #endif
342 
343 #ifndef __BIG_ENDIAN
344  #if defined(BIG_ENDIAN)
345  #define __BIG_ENDIAN BIG_ENDIAN
346  #elif defined(__ORDER_BIG_ENDIAN__)
347  #define __BIG_ENDIAN __ORDER_BIG_ENDIAN__
348  #endif
349 #endif
350 
351 #if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
352  #error "byte order: can't figure out big or little"
353 #endif
354 
355 #ifndef HAVE_PCRE_FREE_STUDY
356 #define pcre_free_study pcre_free
357 #endif
358 
359 #ifndef MIN
360 #define MIN(x, y) (((x)<(y))?(x):(y))
361 #endif
362 
363 #ifndef MAX
364 #define MAX(x, y) (((x)<(y))?(y):(x))
365 #endif
366 
367 #define BIT_U8(n) ((uint8_t)(1 << (n)))
368 #define BIT_U16(n) ((uint16_t)(1 << (n)))
369 #define BIT_U32(n) (1UL << (n))
370 #define BIT_U64(n) (1ULL << (n))
371 
372 #define WARN_UNUSED __attribute__((warn_unused_result))
373 
374 #define SCNtohl(x) (uint32_t)ntohl((x))
375 #define SCNtohs(x) (uint16_t)ntohs((x))
376 
377 /* swap flags if one of them is set, otherwise do nothing. */
378 #define SWAP_FLAGS(flags, a, b) \
379  do { \
380  if (((flags) & ((a)|(b))) == (a)) { \
381  (flags) &= ~(a); \
382  (flags) |= (b); \
383  } else if (((flags) & ((a)|(b))) == (b)) { \
384  (flags) &= ~(b); \
385  (flags) |= (a); \
386  } \
387  } while(0)
388 
389 #define SWAP_VARS(type, a, b) \
390  do { \
391  type t = (a); \
392  (a) = (b); \
393  (b) = t; \
394  } while (0)
395 
411 
414 
415 /** \note update PacketProfileLoggertIdToString if you change anything here */
416 typedef enum {
418 
419  /* TX loggers first for low logger IDs */
445 
465 } LoggerId;
466 
467 #include "util-optimize.h"
468 #include <htp/htp.h>
469 #include "threads.h"
470 #include "tm-threads-common.h"
471 #include "util-debug.h"
472 #include "util-error.h"
473 #include "util-mem.h"
474 #include "detect-engine-alert.h"
475 #include "util-path.h"
476 #include "util-conf.h"
477 
478 #ifdef HAVE_LUA
479 #include <lua.h>
480 #include <lualib.h>
481 #include <lauxlib.h>
482 #endif
483 
484 #ifndef HAVE_STRLCAT
485 size_t strlcat(char *, const char *src, size_t siz);
486 #endif
487 #ifndef HAVE_STRLCPY
488 size_t strlcpy(char *dst, const char *src, size_t siz);
489 #endif
490 #ifndef HAVE_STRPTIME
491 char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict);
492 #endif
493 
494 extern int coverage_unittests;
495 extern int g_ut_modules;
496 extern int g_ut_covered;
497 
498 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
499 
500 #endif /* __SURICATA_COMMON_H__ */
501 
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
LoggerId
char * strptime(const char *__restrict, const char *__restrict, struct tm *__restrict)
Definition: util-strptime.c:97
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
int g_ut_covered
Definition: suricata.c:862
uint16_t src
enum PacketProfileDetectId_ PacketProfileDetectId
uint16_t dst
int coverage_unittests
Definition: suricata.c:860
int g_ut_modules
Definition: suricata.c:861
PacketProfileDetectId_