suricata
output-json-bittorrent-dht.c
Go to the documentation of this file.
1 /* Copyright (C) 2021 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  * Implement JSON/eve logging app-layer BitTorrent DHT.
22  */
23 
24 #include "suricata-common.h"
25 #include "detect.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 #include "rust.h"
46 
47 typedef struct LogBitTorrentDHTFileCtx_ {
48  uint32_t flags;
51 
56 
57 static int JsonBitTorrentDHTLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
58  void *state, void *tx, uint64_t tx_id)
59 {
60  LogBitTorrentDHTLogThread *thread = thread_data;
61 
62  JsonBuilder *js = CreateEveHeader(
63  p, LOG_DIR_PACKET, "bittorrent_dht", NULL, thread->bittorrent_dht_log_ctx->eve_ctx);
64  if (unlikely(js == NULL)) {
65  return TM_ECODE_FAILED;
66  }
67 
68  jb_open_object(js, "bittorrent_dht");
69  if (!rs_bittorrent_dht_logger_log(tx, js)) {
70  goto error;
71  }
72  jb_close(js);
73 
74  OutputJsonBuilderBuffer(js, thread->ctx);
75  jb_free(js);
76 
77  return TM_ECODE_OK;
78 
79 error:
80  jb_free(js);
81  return TM_ECODE_FAILED;
82 }
83 
84 static void OutputBitTorrentDHTLogDeInitCtxSub(OutputCtx *output_ctx)
85 {
86  LogBitTorrentDHTFileCtx *bittorrent_dht_log_ctx = (LogBitTorrentDHTFileCtx *)output_ctx->data;
87  SCFree(bittorrent_dht_log_ctx);
88  SCFree(output_ctx);
89 }
90 
91 static OutputInitResult OutputBitTorrentDHTLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
92 {
93  OutputInitResult result = { NULL, false };
94  OutputJsonCtx *ajt = parent_ctx->data;
95 
96  LogBitTorrentDHTFileCtx *bittorrent_dht_log_ctx = SCCalloc(1, sizeof(*bittorrent_dht_log_ctx));
97  if (unlikely(bittorrent_dht_log_ctx == NULL)) {
98  return result;
99  }
100  bittorrent_dht_log_ctx->eve_ctx = ajt;
101 
102  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
103  if (unlikely(output_ctx == NULL)) {
104  SCFree(bittorrent_dht_log_ctx);
105  return result;
106  }
107  output_ctx->data = bittorrent_dht_log_ctx;
108  output_ctx->DeInit = OutputBitTorrentDHTLogDeInitCtxSub;
109 
111 
112  result.ctx = output_ctx;
113  result.ok = true;
114  return result;
115 }
116 
117 static TmEcode JsonBitTorrentDHTLogThreadInit(ThreadVars *t, const void *initdata, void **data)
118 {
119  LogBitTorrentDHTLogThread *thread = SCCalloc(1, sizeof(*thread));
120  if (unlikely(thread == NULL)) {
121  return TM_ECODE_FAILED;
122  }
123 
124  if (initdata == NULL) {
125  SCLogDebug("Error getting context for EveLogBitTorrentDHT. \"initdata\" is NULL.");
126  goto error_exit;
127  }
128 
129  thread->bittorrent_dht_log_ctx = ((OutputCtx *)initdata)->data;
130  thread->ctx = CreateEveThreadCtx(t, thread->bittorrent_dht_log_ctx->eve_ctx);
131  if (!thread->ctx) {
132  goto error_exit;
133  }
134  *data = (void *)thread;
135 
136  return TM_ECODE_OK;
137 
138 error_exit:
139  SCFree(thread);
140  return TM_ECODE_FAILED;
141 }
142 
143 static TmEcode JsonBitTorrentDHTLogThreadDeinit(ThreadVars *t, void *data)
144 {
146  if (thread == NULL) {
147  return TM_ECODE_OK;
148  }
149  FreeEveThreadCtx(thread->ctx);
150  SCFree(thread);
151  return TM_ECODE_OK;
152 }
153 
155 {
156  if (ConfGetNode("app-layer.protocols.bittorrent-dht") == NULL) {
157  return;
158  }
159 
160  /* Register as an eve sub-module. */
161  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonBitTorrentDHTLog",
162  "eve-log.bittorrent-dht", OutputBitTorrentDHTLogInitSub, ALPROTO_BITTORRENT_DHT,
163  JsonBitTorrentDHTLogger, JsonBitTorrentDHTLogThreadInit,
164  JsonBitTorrentDHTLogThreadDeinit, NULL);
165 }
util-byte.h
tm-threads.h
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
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:357
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:934
LogBitTorrentDHTLogThread_::bittorrent_dht_log_ctx
LogBitTorrentDHTFileCtx * bittorrent_dht_log_ctx
Definition: output-json-bittorrent-dht.c:53
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
LogBitTorrentDHTFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-bittorrent-dht.c:49
rust.h
LogBitTorrentDHTLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-bittorrent-dht.c:54
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
output-json-bittorrent-dht.h
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:477
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:796
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
Packet_
Definition: decode.h:428
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
OutputInitResult_
Definition: output.h:46
JsonBitTorrentDHTLogRegister
void JsonBitTorrentDHTLogRegister(void)
Definition: output-json-bittorrent-dht.c:154
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LogBitTorrentDHTFileCtx_::flags
uint32_t flags
Definition: output-json-bittorrent-dht.c:48
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:456
LogBitTorrentDHTFileCtx
struct LogBitTorrentDHTFileCtx_ LogBitTorrentDHTFileCtx
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
LogBitTorrentDHTLogThread
struct LogBitTorrentDHTLogThread_ LogBitTorrentDHTLogThread
util-buffer.h
ALPROTO_BITTORRENT_DHT
@ ALPROTO_BITTORRENT_DHT
Definition: app-layer-protos.h:62
LogBitTorrentDHTLogThread_
Definition: output-json-bittorrent-dht.c:52
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h
LogBitTorrentDHTFileCtx_
Definition: output-json-bittorrent-dht.c:47