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  if (!rs_bittorrent_dht_logger_log(tx, js)) {
69  goto error;
70  }
71 
72  OutputJsonBuilderBuffer(js, thread->ctx);
73  jb_free(js);
74 
75  return TM_ECODE_OK;
76 
77 error:
78  jb_free(js);
79  return TM_ECODE_FAILED;
80 }
81 
82 static void OutputBitTorrentDHTLogDeInitCtxSub(OutputCtx *output_ctx)
83 {
84  LogBitTorrentDHTFileCtx *bittorrent_dht_log_ctx = (LogBitTorrentDHTFileCtx *)output_ctx->data;
85  SCFree(bittorrent_dht_log_ctx);
86  SCFree(output_ctx);
87 }
88 
89 static OutputInitResult OutputBitTorrentDHTLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
90 {
91  OutputInitResult result = { NULL, false };
92  OutputJsonCtx *ajt = parent_ctx->data;
93 
94  LogBitTorrentDHTFileCtx *bittorrent_dht_log_ctx = SCCalloc(1, sizeof(*bittorrent_dht_log_ctx));
95  if (unlikely(bittorrent_dht_log_ctx == NULL)) {
96  return result;
97  }
98  bittorrent_dht_log_ctx->eve_ctx = ajt;
99 
100  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
101  if (unlikely(output_ctx == NULL)) {
102  SCFree(bittorrent_dht_log_ctx);
103  return result;
104  }
105  output_ctx->data = bittorrent_dht_log_ctx;
106  output_ctx->DeInit = OutputBitTorrentDHTLogDeInitCtxSub;
107 
109 
110  result.ctx = output_ctx;
111  result.ok = true;
112  return result;
113 }
114 
115 static TmEcode JsonBitTorrentDHTLogThreadInit(ThreadVars *t, const void *initdata, void **data)
116 {
117  LogBitTorrentDHTLogThread *thread = SCCalloc(1, sizeof(*thread));
118  if (unlikely(thread == NULL)) {
119  return TM_ECODE_FAILED;
120  }
121 
122  if (initdata == NULL) {
123  SCLogDebug("Error getting context for EveLogBitTorrentDHT. \"initdata\" is NULL.");
124  goto error_exit;
125  }
126 
127  thread->bittorrent_dht_log_ctx = ((OutputCtx *)initdata)->data;
128  thread->ctx = CreateEveThreadCtx(t, thread->bittorrent_dht_log_ctx->eve_ctx);
129  if (!thread->ctx) {
130  goto error_exit;
131  }
132  *data = (void *)thread;
133 
134  return TM_ECODE_OK;
135 
136 error_exit:
137  SCFree(thread);
138  return TM_ECODE_FAILED;
139 }
140 
141 static TmEcode JsonBitTorrentDHTLogThreadDeinit(ThreadVars *t, void *data)
142 {
144  if (thread == NULL) {
145  return TM_ECODE_OK;
146  }
147  FreeEveThreadCtx(thread->ctx);
148  SCFree(thread);
149  return TM_ECODE_OK;
150 }
151 
153 {
154  if (ConfGetNode("app-layer.protocols.bittorrent-dht") == NULL) {
155  return;
156  }
157 
158  /* Register as an eve sub-module. */
159  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonBitTorrentDHTLog",
160  "eve-log.bittorrent-dht", OutputBitTorrentDHTLogInitSub, ALPROTO_BITTORRENT_DHT,
161  JsonBitTorrentDHTLogger, JsonBitTorrentDHTLogThreadInit,
162  JsonBitTorrentDHTLogThreadDeinit, NULL);
163 }
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:79
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:928
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:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:85
OutputJsonThreadCtx_
Definition: output-json.h:87
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:475
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:787
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:436
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:152
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
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:413
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:467
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