suricata
output-eve.h
Go to the documentation of this file.
1 /* Copyright (C) 2024 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  * \brief EVE logging subsystem
22  *
23  * This file will attempt to the main module for EVE logging
24  * sub-system. Currently most of the API resides in output-json.[ch],
25  * but due to some circular dependencies between EVE, and LogFileCtx,
26  * it made it hard to add EVE filetype modules there until some
27  * include issues are figured out.
28  */
29 
30 #ifndef SURICATA_OUTPUT_EVE_H
31 #define SURICATA_OUTPUT_EVE_H
32 
33 #include "suricata-common.h"
34 #include "conf.h"
35 
36 typedef uint32_t ThreadId;
37 
38 /**
39  * \brief Structure used to define an EVE output file type plugin.
40  *
41  * EVE filetypes implement an object with a file-like interface and
42  * are used to output EVE log records to files, syslog, or
43  * database. They can be built-in such as the syslog (see
44  * SyslogInitialize()) and nullsink (see NullLogInitialize()) outputs,
45  * registered by a library user or dynamically loaded as a plugin.
46  *
47  * The life cycle of an EVE filetype is:
48  * - Init: called once for each EVE instance using this filetype
49  * - ThreadInit: called once for each output thread
50  * - Write: called for each log record
51  * - ThreadInit: called once for each output thread on exit
52  * - Deinit: called once for each EVE instance using this filetype on exit
53  *
54  * Examples:
55  * - built-in syslog: \ref src/output-eve-syslog.c
56  * - built-in nullsink: \ref src/output-eve-null.c
57  * - example plugin: \ref examples/plugins/c-json-filetype/filetype.c
58  *
59  * ### Multi-Threaded Note:
60  *
61  * The EVE logging system can be configured by the Suricata user to
62  * run in threaded or non-threaded modes. In the default non-threaded
63  * mode, ThreadInit will only be called once and the filetype does not
64  * need to be concerned with threads.
65  *
66  * However, in **threaded** mode, ThreadInit will be called multiple
67  * times and the filetype needs to be thread aware and thread-safe. If
68  * utilizing a unique resource such as a file for each thread then you
69  * may be naturally thread safe. However, if sharing a single file
70  * handle across all threads then your filetype will have to take care
71  * of locking, etc.
72  */
73 typedef struct SCEveFileType_ {
74  /**
75  * \brief The name of the output, used in the configuration.
76  *
77  * This name is used by the configuration file to specify the EVE
78  * filetype used.
79  *
80  * For example:
81  *
82  * \code{.yaml}
83  * outputs:
84  * - eve-log:
85  * filetype: my-output-name
86  * \endcode
87  */
88  const char *name;
89 
90  /**
91  * \brief Function to initialize this filetype.
92  *
93  * \param conf The ConfNode of the `eve-log` configuration
94  * section this filetype is being initialized for
95  *
96  * \param threaded Flag to specify if the EVE sub-systems is in
97  * threaded mode or not
98  *
99  * \param init_data An output pointer for filetype specific data
100  *
101  * \retval 0 on success, -1 on failure
102  */
103  int (*Init)(const ConfNode *conf, const bool threaded, void **init_data);
104 
105  /**
106  * \brief Initialize thread specific data.
107  *
108  * Initialize any thread specific data. For example, if
109  * implementing a file output you might open the files here, so
110  * you have one output file per thread.
111  *
112  * \param init_data Data setup during Init
113  *
114  * \param thread_id A unique ID to differentiate this thread from
115  * others. If EVE is not in threaded mode this will be called
116  * one with a ThreadId of 0. In threaded mode the ThreadId of
117  * 0 correlates to the main Suricata thread.
118  *
119  * \param thread_data Output pointer for any data required by this
120  * thread.
121  *
122  * \retval 0 on success, -1 on failure
123  */
124  int (*ThreadInit)(const void *init_data, const ThreadId thread_id, void **thread_data);
125 
126  /**
127  * \brief Called for each EVE log record.
128  *
129  * The Write function is called for each log EVE log record. The
130  * provided buffer contains a fully formatted EVE record in JSON
131  * format.
132  *
133  * \param buffer The fully formatted JSON EVE log record
134  *
135  * \param buffer_len The length of the buffer
136  *
137  * \param init_data The data setup in the call to Init
138  *
139  * \param thread_data The data setup in the call to ThreadInit
140  *
141  * \retval 0 on success, -1 on failure
142  */
143  int (*Write)(
144  const char *buffer, const int buffer_len, const void *init_data, void *thread_data);
145 
146  /**
147  * \brief Called to deinitialize each thread.
148  *
149  * This function will be called for each thread. It is where any
150  * resources allocated in ThreadInit should be released.
151  *
152  * \param init_data The data setup in Init
153  *
154  * \param thread_data The data setup in ThreadInit
155  */
156  void (*ThreadDeinit)(const void *init_data, void *thread_data);
157 
158  /**
159  * \brief Final call to deinitialize this filetype.
160  *
161  * Called, usually on exit to deinitialize and free any resources
162  * allocated during Init.
163  *
164  * \param init_data Data setup in the call to Init.
165  */
166  void (*Deinit)(void *init_data);
167 
168  /* Internal list management. */
171 
173 
174 SCEveFileType *SCEveFindFileType(const char *name);
175 
176 #endif
SCEveFindFileType
SCEveFileType * SCEveFindFileType(const char *name)
Definition: output-eve.c:43
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve.h:88
SCEveFileType_::Write
int(* Write)(const char *buffer, const int buffer_len, const void *init_data, void *thread_data)
Called for each EVE log record.
Definition: output-eve.h:143
ThreadId
uint32_t ThreadId
Definition: output-eve.h:36
SCEveFileType_::ThreadDeinit
void(* ThreadDeinit)(const void *init_data, void *thread_data)
Called to deinitialize each thread.
Definition: output-eve.h:156
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *)
Register an Eve file type.
Definition: output-eve.c:61
conf.h
suricata-common.h
SCEveFileType_::Deinit
void(* Deinit)(void *init_data)
Final call to deinitialize this filetype.
Definition: output-eve.h:166
SCEveFileType_::TAILQ_ENTRY
TAILQ_ENTRY(SCEveFileType_) entries
SCEveFileType
struct SCEveFileType_ SCEveFileType
Structure used to define an EVE output file type plugin.
ConfNode_
Definition: conf.h:32
SCEveFileType_::Init
int(* Init)(const ConfNode *conf, const bool threaded, void **init_data)
Function to initialize this filetype.
Definition: output-eve.h:103
SCEveFileType_::ThreadInit
int(* ThreadInit)(const void *init_data, const ThreadId thread_id, void **thread_data)
Initialize thread specific data.
Definition: output-eve.h:124
SCEveFileType_
Structure used to define an EVE output file type plugin.
Definition: output-eve.h:73