From a06a706078cd80f16f51b46c80017e6b2e93e9f3 Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Sun, 31 May 2020 08:18:02 -0400 Subject: [PATCH] output/flow: Improve protocol output handling This commit improves handling of the protocol label by removing an unnecessary copy. Additionally, unknown protocol values are no longer zero-padded. --- src/output-json-flow.c | 17 +++++++++-------- src/output-json-netflow.c | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/output-json-flow.c b/src/output-json-flow.c index 30ae4c2adf..6fc22ce1f3 100644 --- a/src/output-json-flow.c +++ b/src/output-json-flow.c @@ -102,13 +102,6 @@ static JsonBuilder *CreateEveHeaderFromFlow(const Flow *f, const char *event_typ dp = f->sp; } - char proto[16]; - if (SCProtoNameValid(f->proto) == TRUE) { - strlcpy(proto, known_proto[f->proto], sizeof(proto)); - } else { - snprintf(proto, sizeof(proto), "%03" PRIu32, f->proto); - } - /* time */ jb_set_string(jb, "timestamp", timebuf); @@ -160,7 +153,15 @@ static JsonBuilder *CreateEveHeaderFromFlow(const Flow *f, const char *event_typ jb_set_uint(jb, "dest_port", dp); break; } - jb_set_string(jb, "proto", proto); + + if (SCProtoNameValid(f->proto)) { + jb_set_string(jb, "proto", known_proto[f->proto]); + } else { + char proto[4]; + snprintf(proto, sizeof(proto), "%"PRIu8"", f->proto); + jb_set_string(jb, "proto", proto); + } + switch (f->proto) { case IPPROTO_ICMP: case IPPROTO_ICMPV6: diff --git a/src/output-json-netflow.c b/src/output-json-netflow.c index 58ba0b487d..82c74eeb54 100644 --- a/src/output-json-netflow.c +++ b/src/output-json-netflow.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2014 Open Information Security Foundation +/* Copyright (C) 2014-2020 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -106,13 +106,6 @@ static JsonBuilder *CreateEveHeaderFromFlow(const Flow *f, const char *event_typ dp = f->sp; } - char proto[16]; - if (SCProtoNameValid(f->proto) == TRUE) { - strlcpy(proto, known_proto[f->proto], sizeof(proto)); - } else { - snprintf(proto, sizeof(proto), "%03" PRIu32, f->proto); - } - /* time */ jb_set_string(js, "timestamp", timebuf); @@ -164,7 +157,15 @@ static JsonBuilder *CreateEveHeaderFromFlow(const Flow *f, const char *event_typ jb_set_uint(js, "dest_port", dp); break; } - jb_set_string(js, "proto", proto); + + if (SCProtoNameValid(f->proto)) { + jb_set_string(js, "proto", known_proto[f->proto]); + } else { + char proto[4]; + snprintf(proto, sizeof(proto), "%"PRIu8"", f->proto); + jb_set_string(js, "proto", proto); + } + switch (f->proto) { case IPPROTO_ICMP: case IPPROTO_ICMPV6: { -- 2.47.2