From: Philippe Antoine Date: Thu, 23 Mar 2023 08:00:42 +0000 (+0100) Subject: enip: optimized tx iterator X-Git-Tag: suricata-6.0.11~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F8664%2Fhead;p=thirdparty%2Fsuricata.git enip: optimized tx iterator As for SMTP, having a linked list. Ticket: #5927 (cherry picked from commit 4f7426fdcf3ec4cab1e1a9da862f3de342b0d85c) --- diff --git a/src/app-layer-enip.c b/src/app-layer-enip.c index 80695647bb..1df2d7bf72 100644 --- a/src/app-layer-enip.c +++ b/src/app-layer-enip.c @@ -483,6 +483,40 @@ static uint16_t ENIPProbingParser(Flow *f, uint8_t direction, return ALPROTO_ENIP; } +static AppLayerGetTxIterTuple ENIPGetTxIterator(const uint8_t ipproto, const AppProto alproto, + void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *state) +{ + ENIPState *enip_state = (ENIPState *)alstate; + AppLayerGetTxIterTuple no_tuple = { NULL, 0, false }; + if (enip_state) { + ENIPTransaction *tx_ptr; + if (state->un.ptr == NULL) { + tx_ptr = TAILQ_FIRST(&enip_state->tx_list); + } else { + tx_ptr = (ENIPTransaction *)state->un.ptr; + } + if (tx_ptr) { + while (tx_ptr->tx_num < min_tx_id + 1) { + tx_ptr = TAILQ_NEXT(tx_ptr, next); + if (!tx_ptr) { + return no_tuple; + } + } + if (tx_ptr->tx_num >= max_tx_id + 1) { + return no_tuple; + } + state->un.ptr = TAILQ_NEXT(tx_ptr, next); + AppLayerGetTxIterTuple tuple = { + .tx_ptr = tx_ptr, + .tx_id = tx_ptr->tx_num - 1, + .has_next = (state->un.ptr != NULL), + }; + return tuple; + } + } + return no_tuple; +} + /** * \brief Function to register the ENIP protocol parsers and other functions */ @@ -543,6 +577,7 @@ void RegisterENIPUDPParsers(void) ENIPGetTxDetectState, ENIPSetTxDetectState); AppLayerParserRegisterGetTx(IPPROTO_UDP, ALPROTO_ENIP, ENIPGetTx); + AppLayerParserRegisterGetTxIterator(IPPROTO_UDP, ALPROTO_ENIP, ENIPGetTxIterator); AppLayerParserRegisterTxDataFunc(IPPROTO_UDP, ALPROTO_ENIP, ENIPGetTxData); AppLayerParserRegisterGetTxCnt(IPPROTO_UDP, ALPROTO_ENIP, ENIPGetTxCnt); AppLayerParserRegisterTxFreeFunc(IPPROTO_UDP, ALPROTO_ENIP, ENIPStateTransactionFree); @@ -621,6 +656,7 @@ void RegisterENIPTCPParsers(void) ENIPGetTxDetectState, ENIPSetTxDetectState); AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_ENIP, ENIPGetTx); + AppLayerParserRegisterGetTxIterator(IPPROTO_TCP, ALPROTO_ENIP, ENIPGetTxIterator); AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_ENIP, ENIPGetTxData); AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_ENIP, ENIPGetTxCnt); AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_ENIP, ENIPStateTransactionFree);