Coverage Report

Created: 2026-09-06 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/output-json-mdns.c
Line
Count
Source
1
/* Copyright (C) 2025 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
#include "suricata-common.h"
19
#include "conf.h"
20
21
#include "threadvars.h"
22
23
#include "util-debug.h"
24
#include "app-layer-parser.h"
25
#include "output.h"
26
27
#include "output-json.h"
28
#include "output-json-mdns.h"
29
#include "rust.h"
30
31
typedef struct SCDnsLogFileCtx_ {
32
    uint64_t flags; /** Store mode */
33
    OutputJsonCtx *eve_ctx;
34
    uint8_t version;
35
} SCDnsLogFileCtx;
36
37
typedef struct SCDnsLogThread_ {
38
    SCDnsLogFileCtx *dnslog_ctx;
39
    OutputJsonThreadCtx *ctx;
40
} SCDnsLogThread;
41
42
bool AlertJsonMdns(void *txptr, SCJsonBuilder *js)
43
0
{
44
0
    return SCMdnsLogJson(txptr, js);
45
0
}
46
47
static int JsonMdnsLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f,
48
        void *alstate, void *txptr, uint64_t tx_id)
49
0
{
50
0
    SCDnsLogThread *td = (SCDnsLogThread *)thread_data;
51
0
    SCDnsLogFileCtx *dnslog_ctx = td->dnslog_ctx;
52
53
0
    SCJsonBuilder *jb = CreateEveHeader(p, LOG_DIR_FLOW, "mdns", NULL, dnslog_ctx->eve_ctx);
54
0
    if (unlikely(jb == NULL)) {
55
0
        return TM_ECODE_OK;
56
0
    }
57
58
0
    if (SCMdnsLogJson(txptr, jb)) {
59
0
        OutputJsonBuilderBuffer(tv, p, p->flow, jb, td->ctx);
60
0
    }
61
0
    SCJbFree(jb);
62
63
0
    return TM_ECODE_OK;
64
0
}
65
66
static TmEcode SCDnsLogThreadInit(ThreadVars *t, const void *initdata, void **data)
67
0
{
68
0
    SCDnsLogThread *aft = SCCalloc(1, sizeof(SCDnsLogThread));
69
0
    if (unlikely(aft == NULL))
70
0
        return TM_ECODE_FAILED;
71
72
0
    if (initdata == NULL) {
73
0
        SCLogDebug("Error getting log context for eve-log.mdns.  \"initdata\" argument NULL");
74
0
        goto error_exit;
75
0
    }
76
77
    /* Use the Output Context (file pointer and mutex) */
78
0
    aft->dnslog_ctx = ((OutputCtx *)initdata)->data;
79
0
    aft->ctx = CreateEveThreadCtx(t, aft->dnslog_ctx->eve_ctx);
80
0
    if (!aft->ctx) {
81
0
        goto error_exit;
82
0
    }
83
84
0
    *data = (void *)aft;
85
0
    return TM_ECODE_OK;
86
87
0
error_exit:
88
0
    SCFree(aft);
89
0
    return TM_ECODE_FAILED;
90
0
}
91
92
static TmEcode SCDnsLogThreadDeinit(ThreadVars *t, void *data)
93
0
{
94
0
    SCDnsLogThread *aft = (SCDnsLogThread *)data;
95
0
    if (aft == NULL) {
96
0
        return TM_ECODE_OK;
97
0
    }
98
0
    FreeEveThreadCtx(aft->ctx);
99
100
    /* clear memory */
101
0
    memset(aft, 0, sizeof(SCDnsLogThread));
102
103
0
    SCFree(aft);
104
0
    return TM_ECODE_OK;
105
0
}
106
107
static void DnsLogDeInitCtxSub(OutputCtx *output_ctx)
108
0
{
109
0
    SCDnsLogFileCtx *dnslog_ctx = (SCDnsLogFileCtx *)output_ctx->data;
110
0
    SCFree(dnslog_ctx);
111
0
    SCFree(output_ctx);
112
0
}
113
114
static OutputInitResult DnsLogInitCtxSub(SCConfNode *conf, OutputCtx *parent_ctx)
115
0
{
116
0
    OutputInitResult result = { NULL, false };
117
0
    const char *enabled = SCConfNodeLookupChildValue(conf, "enabled");
118
0
    if (enabled != NULL && !SCConfValIsTrue(enabled)) {
119
0
        result.ok = true;
120
0
        return result;
121
0
    }
122
123
0
    OutputJsonCtx *ojc = parent_ctx->data;
124
125
0
    SCDnsLogFileCtx *dnslog_ctx = SCCalloc(1, sizeof(SCDnsLogFileCtx));
126
0
    if (unlikely(dnslog_ctx == NULL)) {
127
0
        return result;
128
0
    }
129
130
0
    dnslog_ctx->eve_ctx = ojc;
131
0
    dnslog_ctx->version = DNS_LOG_VERSION_3;
132
133
    /* For mDNS, log everything.
134
     *
135
     * TODO: Maybe add flags for request and/or response only.
136
     */
137
0
    dnslog_ctx->flags = ~0ULL;
138
139
0
    OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
140
0
    if (unlikely(output_ctx == NULL)) {
141
0
        SCFree(dnslog_ctx);
142
0
        return result;
143
0
    }
144
145
0
    output_ctx->data = dnslog_ctx;
146
0
    output_ctx->DeInit = DnsLogDeInitCtxSub;
147
148
0
    SCAppLayerParserRegisterLogger(IPPROTO_UDP, ALPROTO_MDNS);
149
150
0
    result.ctx = output_ctx;
151
0
    result.ok = true;
152
0
    return result;
153
0
}
154
155
void JsonMdnsLogRegister(void)
156
78
{
157
78
    OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonMdnsLog", "eve-log.mdns",
158
78
            DnsLogInitCtxSub, ALPROTO_MDNS, JsonMdnsLogger, SCDnsLogThreadInit,
159
78
            SCDnsLogThreadDeinit);
160
78
}