/src/suricata7/src/output-json-common.c
Line | Count | Source |
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 | | |
29 | | OutputJsonThreadCtx *CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx) |
30 | 56 | { |
31 | 56 | OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread)); |
32 | 56 | if (unlikely(thread == NULL)) { |
33 | 0 | return NULL; |
34 | 0 | } |
35 | | |
36 | 56 | thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE); |
37 | 56 | if (unlikely(thread->buffer == NULL)) { |
38 | 0 | goto error; |
39 | 0 | } |
40 | | |
41 | 56 | thread->file_ctx = LogFileEnsureExists(ctx->file_ctx); |
42 | 56 | if (!thread->file_ctx) { |
43 | 0 | goto error; |
44 | 0 | } |
45 | | |
46 | 56 | thread->ctx = ctx; |
47 | | |
48 | 56 | return thread; |
49 | | |
50 | 0 | error: |
51 | 0 | if (thread->buffer) { |
52 | 0 | MemBufferFree(thread->buffer); |
53 | 0 | } |
54 | 0 | SCFree(thread); |
55 | 0 | return NULL; |
56 | 56 | } |
57 | | |
58 | | void FreeEveThreadCtx(OutputJsonThreadCtx *ctx) |
59 | 0 | { |
60 | 0 | if (ctx != NULL && ctx->buffer != NULL) { |
61 | 0 | MemBufferFree(ctx->buffer); |
62 | 0 | } |
63 | 0 | if (ctx != NULL) { |
64 | 0 | SCFree(ctx); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx) |
69 | 0 | { |
70 | 0 | SCFree(output_ctx); |
71 | 0 | } |
72 | | |
73 | | OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx) |
74 | 54 | { |
75 | 54 | OutputInitResult result = { NULL, false }; |
76 | | |
77 | 54 | OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx)); |
78 | 54 | if (unlikely(output_ctx == NULL)) { |
79 | 0 | return result; |
80 | 0 | } |
81 | 54 | output_ctx->data = parent_ctx->data; |
82 | 54 | output_ctx->DeInit = OutputJsonLogDeInitCtxSub; |
83 | | |
84 | 54 | result.ctx = output_ctx; |
85 | 54 | result.ok = true; |
86 | 54 | return result; |
87 | 54 | } |
88 | | |
89 | | |
90 | | TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data) |
91 | 64 | { |
92 | 64 | if (initdata == NULL) { |
93 | 0 | return TM_ECODE_FAILED; |
94 | 0 | } |
95 | | |
96 | 64 | OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread)); |
97 | 64 | if (unlikely(thread == NULL)) { |
98 | 0 | return TM_ECODE_FAILED; |
99 | 0 | } |
100 | | |
101 | 64 | thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE); |
102 | 64 | if (unlikely(thread->buffer == NULL)) { |
103 | 0 | goto error_exit; |
104 | 0 | } |
105 | | |
106 | 64 | thread->ctx = ((OutputCtx *)initdata)->data; |
107 | 64 | thread->file_ctx = LogFileEnsureExists(thread->ctx->file_ctx); |
108 | 64 | if (!thread->file_ctx) { |
109 | 0 | goto error_exit; |
110 | 0 | } |
111 | | |
112 | 64 | *data = (void *)thread; |
113 | 64 | return TM_ECODE_OK; |
114 | | |
115 | 0 | error_exit: |
116 | 0 | if (thread->buffer) { |
117 | 0 | MemBufferFree(thread->buffer); |
118 | 0 | } |
119 | 0 | SCFree(thread); |
120 | 0 | return TM_ECODE_FAILED; |
121 | 64 | } |
122 | | |
123 | | TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data) |
124 | 0 | { |
125 | 0 | OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data; |
126 | 0 | FreeEveThreadCtx(thread); |
127 | 0 | return TM_ECODE_OK; |
128 | 0 | } |