From: Victor Julien Date: Sun, 12 Nov 2023 08:41:13 +0000 (+0100) Subject: frames: add FrameGetLastOpenByType X-Git-Tag: suricata-8.0.0-beta1~1205 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=803e8dd32e73b6f31debeae5601ce8e07e77a9d4;p=thirdparty%2Fsuricata.git frames: add FrameGetLastOpenByType Getter for the most recent frame with unknown length (-1). --- diff --git a/src/app-layer-frames.c b/src/app-layer-frames.c index b8554f59a9..6953d26c9a 100644 --- a/src/app-layer-frames.c +++ b/src/app-layer-frames.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2022 Open Information Security Foundation +/* Copyright (C) 2007-2024 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 @@ -83,6 +83,32 @@ static void FrameDebug(const char *prefix, const Frames *frames, const Frame *fr #endif } +/** + * \note "open" means a frame that has no length set (len == -1) + * \todo perhaps we can search backwards */ +Frame *FrameGetLastOpenByType(Frames *frames, const uint8_t frame_type) +{ + Frame *candidate = NULL; + + SCLogDebug( + "frames %p cnt %u, looking for last of type %" PRIu8, frames, frames->cnt, frame_type); + for (uint16_t i = 0; i < frames->cnt; i++) { + if (i < FRAMES_STATIC_CNT) { + Frame *frame = &frames->sframes[i]; + FrameDebug("get_by_id(static)", frames, frame); + if (frame->type == frame_type && frame->len == -1) + candidate = frame; + } else { + const uint16_t o = i - FRAMES_STATIC_CNT; + Frame *frame = &frames->dframes[o]; + FrameDebug("get_by_id(dynamic)", frames, frame); + if (frame->type == frame_type && frame->len == -1) + candidate = frame; + } + } + return candidate; +} + Frame *FrameGetById(Frames *frames, const int64_t id) { SCLogDebug("frames %p cnt %u, looking for %" PRIi64, frames, frames->cnt, id); diff --git a/src/app-layer-frames.h b/src/app-layer-frames.h index 1904917b42..b78077fd2b 100644 --- a/src/app-layer-frames.h +++ b/src/app-layer-frames.h @@ -88,6 +88,7 @@ void AppLayerFrameDump(Flow *f); Frame *FrameGetByIndex(Frames *frames, const uint32_t idx); Frame *FrameGetById(Frames *frames, const int64_t id); +Frame *FrameGetLastOpenByType(Frames *frames, const uint8_t frame_type); Frame *AppLayerFrameGetById(Flow *f, const int direction, const FrameId frame_id); FrameId AppLayerFrameGetId(Frame *r);