Skip to content

Commit f674241

Browse files
glongojasonish
authored andcommitted
output/json: check 5-tuple values prior to logging
This commit enhances the JSON output by introducing a feature for conditional port logging. Now, port logging is dependent on the underlying protocol (such as TCP, UDP, or SCTP), where port information is pertinent, while it avoids unnecessary logging for protocols where a port is not utilized (e.g. ARP). Furthermore, this update ensures that IP addresses and the protocol have meaningful values set, rather than being logged as empty strings. These changes will make each log entry more precise, eliminating cases where 5-tuple fields are empty or set to zero, indicating the absence of a field. (cherry picked from commit a1c6328)
1 parent e5ebe36 commit f674241

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/output-json.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,10 @@ void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddr
581581
case IPPROTO_SCTP:
582582
addr->sp = sp;
583583
addr->dp = dp;
584+
addr->log_port = true;
584585
break;
585586
default:
587+
addr->log_port = false;
586588
break;
587589
}
588590

@@ -880,11 +882,21 @@ JsonBuilder *CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir,
880882
JsonAddrInfoInit(p, dir, &addr_info);
881883
addr = &addr_info;
882884
}
883-
jb_set_string(js, "src_ip", addr->src_ip);
884-
jb_set_uint(js, "src_port", addr->sp);
885-
jb_set_string(js, "dest_ip", addr->dst_ip);
886-
jb_set_uint(js, "dest_port", addr->dp);
887-
jb_set_string(js, "proto", addr->proto);
885+
if (addr->src_ip[0] != '\0') {
886+
jb_set_string(js, "src_ip", addr->src_ip);
887+
}
888+
if (addr->log_port) {
889+
jb_set_uint(js, "src_port", addr->sp);
890+
}
891+
if (addr->dst_ip[0] != '\0') {
892+
jb_set_string(js, "dest_ip", addr->dst_ip);
893+
}
894+
if (addr->log_port) {
895+
jb_set_uint(js, "dest_port", addr->dp);
896+
}
897+
if (addr->proto[0] != '\0') {
898+
jb_set_string(js, "proto", addr->proto);
899+
}
888900

889901
/* icmp */
890902
switch (p->proto) {

src/output-json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ typedef struct JsonAddrInfo_ {
5252
Port sp;
5353
Port dp;
5454
char proto[JSON_PROTO_LEN];
55+
// Ports are logged only when provided by the transport protocol.
56+
bool log_port;
5557
} JsonAddrInfo;
5658

5759
extern const JsonAddrInfo json_addr_info_zero;

0 commit comments

Comments
 (0)