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(t->id, ctx->file_ctx);
42 if (!thread->file_ctx) {
43 goto error;
44 }
45
46 thread->ctx = ctx;
47
48 return thread;
49
50error:
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
68static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx)
69{
70 SCFree(output_ctx);
71}
72
73int OutputJsonLogFlush(ThreadVars *tv, void *thread_data, const Packet *p)
74{
75 OutputJsonThreadCtx *aft = thread_data;
76 LogFileCtx *file_ctx = aft->ctx->file_ctx;
77 SCLogDebug("%s flushing %s", tv->name, file_ctx->filename);
78 LogFileFlush(file_ctx);
79 return 0;
80}
81
83{
84 OutputInitResult result = { NULL, false };
85
86 OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
87 if (unlikely(output_ctx == NULL)) {
88 return result;
89 }
90 output_ctx->data = parent_ctx->data;
91 output_ctx->DeInit = OutputJsonLogDeInitCtxSub;
92
93 result.ctx = output_ctx;
94 result.ok = true;
95 return result;
96}
97
98
99TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
100{
101 if (initdata == NULL) {
102 return TM_ECODE_FAILED;
103 }
104
105 OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread));
106 if (unlikely(thread == NULL)) {
107 return TM_ECODE_FAILED;
108 }
109
111 if (unlikely(thread->buffer == NULL)) {
112 goto error_exit;
113 }
114
115 thread->ctx = ((OutputCtx *)initdata)->data;
116 thread->file_ctx = LogFileEnsureExists(t->id, thread->ctx->file_ctx);
117 if (!thread->file_ctx) {
118 goto error_exit;
119 }
120
121 *data = (void *)thread;
122 return TM_ECODE_OK;
123
124error_exit:
125 if (thread->buffer) {
126 MemBufferFree(thread->buffer);
127 }
128 SCFree(thread);
129 return TM_ECODE_FAILED;
130}
131
133{
135 FreeEveThreadCtx(thread);
136 return TM_ECODE_OK;
137}
ThreadVars * tv
struct Thresholds ctx
int OutputJsonLogFlush(ThreadVars *tv, void *thread_data, const Packet *p)
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
OutputInitResult OutputJsonLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
#define JSON_OUTPUT_BUFFER_SIZE
Definition output-json.h:56
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
LogFileCtx * file_ctx
Definition output-json.h:76
LogFileCtx * file_ctx
Definition output-json.h:85
OutputJsonCtx * ctx
Definition output-json.h:84
Per thread variable structure.
Definition threadvars.h:58
char name[16]
Definition threadvars.h:65
@ TM_ECODE_FAILED
@ TM_ECODE_OK
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition util-buffer.c:32
void MemBufferFree(MemBuffer *buffer)
Definition util-buffer.c:86
#define SCLogDebug(...)
Definition util-debug.h:275
LogFileCtx * LogFileEnsureExists(ThreadId thread_id, LogFileCtx *parent_ctx)
LogFileEnsureExists() Ensure a log file context for the thread exists.
void LogFileFlush(LogFileCtx *file_ctx)
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)