suricata
output-json-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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  SCFree(thread);
89  return TM_ECODE_FAILED;
90  }
91 
92  thread->ctx = ((OutputCtx *)initdata)->data;
93  *data = (void *)thread;
94  return TM_ECODE_OK;
95 }
96 
98 {
99  OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data;
100  if (thread == NULL) {
101  return TM_ECODE_OK;
102  }
103  if (thread->buffer != NULL) {
104  MemBufferFree(thread->buffer);
105  }
106  SCFree(thread);
107  return TM_ECODE_OK;
108 }
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
OutputJsonCtx * ctx
Definition: output-json.h:86
#define unlikely(expr)
Definition: util-optimize.h:35
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
#define SCCalloc(nm, a)
Definition: util-mem.h:253
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
void * data
Definition: tm-modules.h:81
MemBuffer * buffer
Definition: output-json.h:87
Per thread variable structure.
Definition: threadvars.h:57
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82