From d362e45d72b84f2f7743c4740e40bfd88eb33970 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Tue, 13 Dec 2022 09:09:44 +0100 Subject: [PATCH] smb: convert transaction list to vecdeque Allows for more efficient removal from front of the list. Ticket: #5753 (cherry picked from commit 1d9183638f930e8e0f22c421ee0ef9fde043106a) --- rust/src/smb/dcerpc.rs | 8 ++++---- rust/src/smb/debug.rs | 4 ++-- rust/src/smb/files.rs | 4 ++-- rust/src/smb/session.rs | 4 ++-- rust/src/smb/smb.rs | 36 +++++++++++++++++++----------------- rust/src/smb/smb2_ioctl.rs | 4 ++-- 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/rust/src/smb/dcerpc.rs b/rust/src/smb/dcerpc.rs index 068e496e8a..2e591cb0c9 100644 --- a/rust/src/smb/dcerpc.rs +++ b/rust/src/smb/dcerpc.rs @@ -144,8 +144,8 @@ impl SMBState { SMBTransactionDCERPC::new_request(cmd, call_id))); SCLogDebug!("SMB: TX DCERPC created: ID {} hdr {:?}", tx.id, tx.hdr); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } @@ -159,8 +159,8 @@ impl SMBState { SMBTransactionDCERPC::new_response(call_id))); SCLogDebug!("SMB: TX DCERPC created: ID {} hdr {:?}", tx.id, tx.hdr); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } diff --git a/rust/src/smb/debug.rs b/rust/src/smb/debug.rs index 15d8748c0a..287a40f9e0 100644 --- a/rust/src/smb/debug.rs +++ b/rust/src/smb/debug.rs @@ -24,8 +24,8 @@ impl SMBState { #[cfg(feature = "debug")] pub fn _debug_tx_stats(&self) { if self.transactions.len() > 1 { - let txf = self.transactions.first().unwrap(); - let txl = self.transactions.last().unwrap(); + let txf = self.transactions.front().unwrap(); + let txl = self.transactions.back().unwrap(); SCLogDebug!("TXs {} MIN {} MAX {}", self.transactions.len(), txf.id, txl.id); SCLogDebug!("- OLD tx.id {}: {:?}", txf.id, txf); diff --git a/rust/src/smb/files.rs b/rust/src/smb/files.rs index e2aa640338..99b52fc9cd 100644 --- a/rust/src/smb/files.rs +++ b/rust/src/smb/files.rs @@ -111,8 +111,8 @@ impl SMBState { tx.tx_data.init_files_opened(); SCLogDebug!("SMB: new_file_tx: TX FILE created: ID {} NAME {}", tx.id, String::from_utf8_lossy(file_name)); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); let (files, flags) = self.files.get(direction); return (tx_ref.unwrap(), files, flags) } diff --git a/rust/src/smb/session.rs b/rust/src/smb/session.rs index 43883a77d5..437aad4d88 100644 --- a/rust/src/smb/session.rs +++ b/rust/src/smb/session.rs @@ -52,8 +52,8 @@ impl SMBState { tx.response_done = self.tc_trunc; // no response expected if tc is truncated SCLogDebug!("SMB: TX SESSIONSETUP created: ID {}", tx.id); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index 66e12ba406..4d665a47e3 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -34,6 +34,8 @@ use std::collections::HashMap; use nom; +use std::collections::VecDeque; + use crate::core::*; use crate::applayer; use crate::applayer::{AppLayerResult, AppLayerTxData}; @@ -413,8 +415,8 @@ impl SMBState { tx.response_done = self.tc_trunc; // no response expected if tc is truncated SCLogDebug!("SMB: TX SETFILEPATHINFO created: ID {}", tx.id); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } @@ -432,8 +434,8 @@ impl SMBState { tx.response_done = self.tc_trunc; // no response expected if tc is truncated SCLogDebug!("SMB: TX SETFILEPATHINFO created: ID {}", tx.id); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } } @@ -465,8 +467,8 @@ impl SMBState { tx.response_done = self.tc_trunc; // no response expected if tc is truncated SCLogDebug!("SMB: TX RENAME created: ID {}", tx.id); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } } @@ -809,7 +811,7 @@ pub struct SMBState<> { post_gap_files_checked: bool, /// transactions list - pub transactions: Vec, + pub transactions: VecDeque, /// tx counter for assigning incrementing id's to tx's tx_id: u64, @@ -855,7 +857,7 @@ impl SMBState { tc_trunc: false, check_post_gap_file_txs: false, post_gap_files_checked: false, - transactions: Vec::new(), + transactions: VecDeque::new(), tx_id:0, dialect:0, dialect_vec: None, @@ -981,15 +983,15 @@ impl SMBState { SCLogDebug!("SMB: TX GENERIC created: ID {} tx list {} {:?}", tx.id, self.transactions.len(), &tx); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } pub fn get_last_tx(&mut self, smb_ver: u8, smb_cmd: u16) -> Option<&mut SMBTransaction> { - let tx_ref = self.transactions.last_mut(); + let tx_ref = self.transactions.back_mut(); match tx_ref { Some(tx) => { let found = if tx.vercmd.get_version() == smb_ver { @@ -1054,8 +1056,8 @@ impl SMBState { tx.response_done = self.tc_trunc; // no response expected if tc is truncated SCLogDebug!("SMB: TX NEGOTIATE created: ID {} SMB ver {}", tx.id, smb_ver); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } @@ -1093,8 +1095,8 @@ impl SMBState { SCLogDebug!("SMB: TX TREECONNECT created: ID {} NAME {}", tx.id, String::from_utf8_lossy(&name)); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } @@ -1127,8 +1129,8 @@ impl SMBState { tx.request_done = true; tx.response_done = self.tc_trunc; // no response expected if tc is truncated - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } diff --git a/rust/src/smb/smb2_ioctl.rs b/rust/src/smb/smb2_ioctl.rs index 835672f752..1bbf7766f7 100644 --- a/rust/src/smb/smb2_ioctl.rs +++ b/rust/src/smb/smb2_ioctl.rs @@ -49,8 +49,8 @@ impl SMBState { SCLogDebug!("SMB: TX IOCTL created: ID {} FUNC {:08x}: {}", tx.id, func, &fsctl_func_to_string(func)); - self.transactions.push(tx); - let tx_ref = self.transactions.last_mut(); + self.transactions.push_back(tx); + let tx_ref = self.transactions.back_mut(); return tx_ref.unwrap(); } } -- 2.47.2