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