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 "rust.h"
35 #include "conf.h"
36 
37 typedef uint32_t ThreadId;
38 
39 /** \brief Structure used to define an EVE output file type.
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  * - ThreadDeinit: 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 SCConfNode *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  * once 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 /** \brief Function type for EVE callbacks.
177  *
178  * The function type for callbacks registered with
179  * SCEveRegisterCallback. This function will be called with the
180  * SCJsonBuilder just prior to the top-level object being closed. New
181  * fields may be added, however, there is no way to alter existing
182  * objects already added to the SCJsonBuilder.
183  *
184  * \param tv The ThreadVars for the thread performing the logging.
185  * \param p Packet if available.
186  * \param f Flow if available.
187  * \param user User data provided during callback registration.
188  */
189 typedef void (*SCEveUserCallbackFn)(
190  ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb, void *user);
191 
192 /** \brief Register a callback for adding extra information to EVE logs.
193  *
194  * Allow users to register a callback for each EVE log. The callback
195  * is called just before the root object on the SCJsonBuilder is to be
196  * closed.
197  *
198  * New objects and fields can be appended, but existing entries cannot be modified.
199  *
200  * Packet and Flow will be provided if available, but will otherwise be
201  * NULL.
202  *
203  * Limitations: At this time the callbacks will only be called for EVE
204  * loggers that use SCJsonBuilder, notably this means it won't be called
205  * for stats records at this time.
206  *
207  * \returns true if callback is registered, false is not due to memory
208  * allocation error.
209  */
211 
212 /** \internal
213  *
214  * Run EVE callbacks.
215  */
216 void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb);
217 
218 #endif
SCEveRunCallbacks
void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb)
Definition: output-eve.c:53
SCEveRegisterCallback
bool SCEveRegisterCallback(SCEveUserCallbackFn fn, void *user)
Register a callback for adding extra information to EVE logs.
SCEveFindFileType
SCEveFileType * SCEveFindFileType(const char *name)
Definition: output-eve.c:82
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve.h:88
Flow_
Flow data structure.
Definition: flow.h:348
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
rust.h
ThreadId
uint32_t ThreadId
Definition: output-eve.h:37
SCEveFileType_::ThreadDeinit
void(* ThreadDeinit)(const void *init_data, void *thread_data)
Called to deinitialize each thread.
Definition: output-eve.h:156
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *)
Register an Eve file type.
Definition: output-eve.c:100
Packet_
Definition: decode.h:501
conf.h
name
const char * name
Definition: tm-threads.c:2163
SCEveFileType_::Init
int(* Init)(const SCConfNode *conf, const bool threaded, void **init_data)
Function to initialize this filetype.
Definition: output-eve.h:103
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
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
SCEveFileType
struct SCEveFileType_ SCEveFileType
Structure used to define an EVE output file type.
SCEveFileType_::ThreadInit
int(* ThreadInit)(const void *init_data, const ThreadId thread_id, void **thread_data)
Initialize thread specific data.
Definition: output-eve.h:124
SCEveUserCallbackFn
void(* SCEveUserCallbackFn)(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb, void *user)
Function type for EVE callbacks.
Definition: output-eve.h:189
SCConfNode_
Definition: conf.h:37
SCEveFileType_
Structure used to define an EVE output file type.
Definition: output-eve.h:73