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 
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 
73 int 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 
99 TmEcode 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 
124 error_exit:
125  if (thread->buffer) {
126  MemBufferFree(thread->buffer);
127  }
128  SCFree(thread);
129  return TM_ECODE_FAILED;
130 }
131 
133 {
134  OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data;
135  FreeEveThreadCtx(thread);
136  return TM_ECODE_OK;
137 }
OutputJsonLogFlush
int OutputJsonLogFlush(ThreadVars *tv, void *thread_data, const Packet *p)
Definition: output-json-common.c:73
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:82
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:90
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
LogFileFlush
void LogFileFlush(LogFileCtx *file_ctx)
Definition: util-logopenfile.c:977
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:99
OutputJsonCtx_
Definition: output-json.h:81
ctx
struct Thresholds ctx
LogFileCtx_
Definition: util-logopenfile.h:72
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
LogFileCtx_::filename
char * filename
Definition: util-logopenfile.h:106
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:84
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:58
OutputInitResult_::ok
bool ok
Definition: output.h:48
ThreadVars_::id
int id
Definition: threadvars.h:87
Packet_
Definition: decode.h:476
TmEcode
TmEcode
Definition: tm-threads-common.h:79
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
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(ThreadId thread_id, LogFileCtx *parent_ctx)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:749
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:86
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
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:132
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
output.h