Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/output-json-metadata.c
Line
Count
Source
1
/* Copyright (C) 2013-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
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 * Logs vars in JSON format.
24
 *
25
 */
26
27
#include "suricata-common.h"
28
#include "detect.h"
29
#include "flow.h"
30
#include "conf.h"
31
32
#include "threads.h"
33
#include "tm-threads.h"
34
#include "threadvars.h"
35
#include "util-debug.h"
36
37
#include "util-misc.h"
38
#include "util-unittest.h"
39
#include "util-unittest-helper.h"
40
41
#include "detect-parse.h"
42
#include "detect-engine.h"
43
#include "detect-engine-mpm.h"
44
#include "detect-reference.h"
45
#include "app-layer-parser.h"
46
#include "app-layer-dnp3.h"
47
#include "app-layer-htp.h"
48
#include "app-layer-htp-xff.h"
49
#include "util-classification-config.h"
50
#include "util-syslog.h"
51
#include "util-logopenfile.h"
52
53
#include "output.h"
54
#include "output-json.h"
55
#include "output-json-metadata.h"
56
57
#include "util-byte.h"
58
#include "util-privs.h"
59
#include "util-print.h"
60
#include "util-proto-name.h"
61
#include "util-optimize.h"
62
#include "util-buffer.h"
63
64
156
#define MODULE_NAME "JsonMetadataLog"
65
66
static int MetadataJson(ThreadVars *tv, OutputJsonThreadCtx *aft, const Packet *p)
67
4
{
68
4
    SCJsonBuilder *js = CreateEveHeader(p, LOG_DIR_PACKET, "metadata", NULL, aft->ctx);
69
4
    if (unlikely(js == NULL))
70
0
        return TM_ECODE_OK;
71
72
    /* If metadata is not enabled for eve, explicitly log it here as this is
73
     * what logging metadata is about. */
74
4
    if (!aft->ctx->cfg.include_metadata) {
75
0
        EveAddMetadata(p, p->flow, js);
76
0
    }
77
4
    OutputJsonBuilderBuffer(tv, p, p->flow, js, aft);
78
79
4
    SCJbFree(js);
80
4
    return TM_ECODE_OK;
81
4
}
82
83
static int JsonMetadataLogger(ThreadVars *tv, void *thread_data, const Packet *p)
84
4
{
85
4
    OutputJsonThreadCtx *aft = thread_data;
86
87
4
    return MetadataJson(tv, aft, p);
88
4
}
89
90
static bool JsonMetadataLogCondition(ThreadVars *tv, void *data, const Packet *p)
91
14.5M
{
92
14.5M
    return p->pktvar != NULL;
93
14.5M
}
94
95
void JsonMetadataLogRegister (void)
96
78
{
97
78
    OutputPacketLoggerFunctions output_logger_functions = {
98
78
        .LogFunc = JsonMetadataLogger,
99
78
        .ConditionFunc = JsonMetadataLogCondition,
100
78
        .ThreadInitFunc = JsonLogThreadInit,
101
78
        .ThreadDeinitFunc = JsonLogThreadDeinit,
102
78
        .ThreadExitPrintStatsFunc = NULL,
103
78
    };
104
105
78
    OutputRegisterPacketSubModule(LOGGER_JSON_METADATA, "eve-log", MODULE_NAME, "eve-log.metadata",
106
78
            OutputJsonLogInitSub, &output_logger_functions);
107
108
    /* Kept for compatibility. */
109
78
    OutputRegisterPacketSubModule(LOGGER_JSON_METADATA, "eve-log", MODULE_NAME, "eve-log.vars",
110
78
            OutputJsonLogInitSub, &output_logger_functions);
111
78
}