suricata
output-json-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 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 
24 #include "suricata-common.h"
25 #include "output.h"
26 #include "output-json.h"
27 #include "util-buffer.h"
28 
30 {
31  OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread));
32  if (unlikely(thread == NULL)) {
33  return NULL;
34  }
35 
37  if (unlikely(thread->buffer == NULL)) {
38  goto error;
39  }
40 
41  thread->file_ctx = LogFileEnsureExists(ctx->file_ctx, t->id);
42  if (!thread->file_ctx) {
43  goto error;
44  }
45 
46  thread->ctx = ctx;
47 
48  return thread;
49 
50 error:
51  if (thread->buffer) {
52  MemBufferFree(thread->buffer);
53  }
54  SCFree(thread);
55  return NULL;
56 }
57 
59 {
60  if (ctx != NULL && ctx->buffer != NULL) {
61  MemBufferFree(ctx->buffer);
62  }
63  if (ctx != NULL) {
64  SCFree(ctx);
65  }
66 }
67 
68 static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx)
69 {
70  SCFree(output_ctx);
71 }
72 
74 {
75  OutputInitResult result = { NULL, false };
76 
77  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
78  if (unlikely(output_ctx == NULL)) {
79  return result;
80  }
81  output_ctx->data = parent_ctx->data;
82  output_ctx->DeInit = OutputJsonLogDeInitCtxSub;
83 
84  result.ctx = output_ctx;
85  result.ok = true;
86  return result;
87 }
88 
89 
90 TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
91 {
92  if (initdata == NULL) {
93  return TM_ECODE_FAILED;
94  }
95 
96  OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread));
97  if (unlikely(thread == NULL)) {
98  return TM_ECODE_FAILED;
99  }
100 
102  if (unlikely(thread->buffer == NULL)) {
103  goto error_exit;
104  }
105 
106  thread->ctx = ((OutputCtx *)initdata)->data;
107  thread->file_ctx = LogFileEnsureExists(thread->ctx->file_ctx, t->id);
108  if (!thread->file_ctx) {
109  goto error_exit;
110  }
111 
112  *data = (void *)thread;
113  return TM_ECODE_OK;
114 
115 error_exit:
116  if (thread->buffer) {
117  MemBufferFree(thread->buffer);
118  }
119  SCFree(thread);
120  return TM_ECODE_FAILED;
121 }
122 
124 {
125  OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data;
126  FreeEveThreadCtx(thread);
127  return TM_ECODE_OK;
128 }
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:73
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:90
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:90
OutputJsonCtx_
Definition: output-json.h:81
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:664
OutputInitResult_::ok
bool ok
Definition: output.h:48
ThreadVars_::id
int id
Definition: threadvars.h:86
TmEcode
TmEcode
Definition: tm-threads-common.h:83
OutputJsonThreadCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:91
OutputInitResult_
Definition: output.h:46
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:92
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:82
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:123
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
output.h