Skip to content

Commit

Permalink
smb: convert transaction list to vecdeque
Browse files Browse the repository at this point in the history
Allows for more efficient removal from front of the list.

Ticket: OISF#5753
  • Loading branch information
catenacyber authored and victorjulien committed Jan 10, 2023
1 parent 8c31074 commit 1d91836
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 30 deletions.
8 changes: 4 additions & 4 deletions rust/src/smb/dcerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,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();
}

Expand All @@ -148,8 +148,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();
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/smb/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions rust/src/smb/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ impl SMBState {
tx.tx_data.file_tx = if direction == Direction::ToServer { STREAM_TOSERVER } else { STREAM_TOCLIENT }; // TODO direction to flag func?
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();
return tx_ref.unwrap();
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/smb/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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();
}

Expand Down
37 changes: 19 additions & 18 deletions rust/src/smb/smb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use std::str;
use std::ffi::{self, CString};

use std::collections::HashMap;

use std::collections::VecDeque;

use nom7::{Err, Needed};
use nom7::error::{make_error, ErrorKind};

Expand Down Expand Up @@ -334,8 +335,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();
}

Expand All @@ -353,8 +354,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();
}
}
Expand Down Expand Up @@ -386,8 +387,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();
}
}
Expand Down Expand Up @@ -712,7 +713,7 @@ pub struct SMBState<> {
post_gap_files_checked: bool,

/// transactions list
pub transactions: Vec<SMBTransaction>,
pub transactions: VecDeque<SMBTransaction>,

/// tx counter for assigning incrementing id's to tx's
tx_id: u64,
Expand Down Expand Up @@ -768,7 +769,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,
Expand Down Expand Up @@ -872,15 +873,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();
if let Some(tx) = tx_ref {
let found = if tx.vercmd.get_version() == smb_ver {
if smb_ver == 1 {
Expand Down Expand Up @@ -942,8 +943,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();
}

Expand Down Expand Up @@ -977,8 +978,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();
}

Expand Down Expand Up @@ -1011,8 +1012,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();
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/smb/smb2_ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,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();
}
}
Expand Down

0 comments on commit 1d91836

Please sign in to comment.