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 #ifdef HAVE_LIBJANSSON
45 
46 static void OutputJsonLogDeInitCtxSub(OutputCtx *output_ctx)
47 {
48  SCFree(output_ctx->data);
49  SCFree(output_ctx);
50 }
51 
52 OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
53 {
54  OutputInitResult result = { NULL, false };
55  OutputJsonCtx *ajt = parent_ctx->data;
56 
57  OutputJsonCtx *log_ctx = SCCalloc(1, sizeof(*log_ctx));
58  if (unlikely(log_ctx == NULL)) {
59  return result;
60  }
61  *log_ctx = *ajt;
62 
63  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
64  if (unlikely(output_ctx == NULL)) {
65  SCFree(log_ctx);
66  return result;
67  }
68  output_ctx->data = log_ctx;
69  output_ctx->DeInit = OutputJsonLogDeInitCtxSub;
70 
71  result.ctx = output_ctx;
72  result.ok = true;
73  return result;
74 }
75 
76 
77 TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
78 {
79  if (initdata == NULL) {
80  return TM_ECODE_FAILED;
81  }
82 
83  OutputJsonThreadCtx *thread = SCCalloc(1, sizeof(*thread));
84  if (unlikely(thread == NULL)) {
85  return TM_ECODE_FAILED;
86  }
87 
88  thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
89  if (unlikely(thread->buffer == NULL)) {
90  SCFree(thread);
91  return TM_ECODE_FAILED;
92  }
93 
94  thread->ctx = ((OutputCtx *)initdata)->data;
95  *data = (void *)thread;
96  return TM_ECODE_OK;
97 }
98 
99 TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
100 {
101  OutputJsonThreadCtx *thread = (OutputJsonThreadCtx *)data;
102  if (thread == NULL) {
103  return TM_ECODE_OK;
104  }
105  if (thread->buffer != NULL) {
106  MemBufferFree(thread->buffer);
107  }
108  SCFree(thread);
109  return TM_ECODE_OK;
110 }
111 
112 #endif /* HAVE_LIBJANSSON */
113 
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define unlikely(expr)
Definition: util-optimize.h:35
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
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
Per thread variable structure.
Definition: threadvars.h:57
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82