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 
44 static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx)
45 {
46  SCFree(output_ctx->data);
47  SCFree(output_ctx);
48 }
49 
51 {
52  OutputInitResult result = { NULL, false };
53  OutputJsonCtx *ajt = parent_ctx->data;
54 
55  OutputJsonCtx *log_ctx = SCCalloc(1, sizeof(*log_ctx));
56  if (unlikely(log_ctx == NULL)) {
57  return result;
58  }
59  *log_ctx = *ajt;
60 
61  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
62  if (unlikely(output_ctx == NULL)) {
63  SCFree(log_ctx);
64  return result;
65  }
66  output_ctx->data = log_ctx;
67  output_ctx->DeInit = OutputJsonLogDeInitCtxSub;
68 
69  result.ctx = output_ctx;
70  result.ok = true;
71  return result;
72 }
73 
74 
75 TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
76 {
77  if (initdata == NULL) {
78  return TM_ECODE_FAILED;
79  }
80 
81  OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread));
82  if (unlikely(thread == NULL)) {
83  return TM_ECODE_FAILED;
84  }
85 
87  if (unlikely(thread->buffer == NULL)) {
88  goto error_exit;
89  }
90 
91  thread->ctx = ((OutputCtx *)initdata)->data;
92  LogFileEnsureExists(thread->ctx->file_ctx, t->id);
93  thread->file_ctx = LogFileEnsureExists(thread->ctx->file_ctx, t->id);
94  if (!thread->file_ctx) {
95  goto error_exit;
96  }
97 
98  *data = (void *)thread;
99  return TM_ECODE_OK;
100 
101 error_exit:
102  if (thread->buffer) {
103  MemBufferFree(thread->buffer);
104  }
105  SCFree(thread);
106  return TM_ECODE_FAILED;
107 }
108 
110 {
111  OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data;
112  if (thread == NULL) {
113  return TM_ECODE_OK;
114  }
115  if (thread->buffer != NULL) {
116  MemBufferFree(thread->buffer);
117  }
118  SCFree(thread);
119  return TM_ECODE_OK;
120 }
util-byte.h
tm-threads.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:50
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:110
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
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:75
threads.h
OutputJsonCtx_
Definition: output-json.h:101
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:109
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:659
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:111
OutputInitResult_
Definition: output.h:43
OutputJsonThreadCtx_::buffer
MemBuffer * buffer
Definition: output-json.h:112
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:102
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:109
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