diff --git a/rust/src/dcerpc/dcerpc_udp.rs b/rust/src/dcerpc/dcerpc_udp.rs index d34c3e480b36..b17cc8d1fa8b 100644 --- a/rust/src/dcerpc/dcerpc_udp.rs +++ b/rust/src/dcerpc/dcerpc_udp.rs @@ -141,13 +141,12 @@ impl DCERPCUDPState { } fn find_incomplete_tx(&mut self, hdr: &DCERPCHdrUdp) -> Option<&mut DCERPCTransaction> { - for tx in &mut self.transactions { - if tx.seqnum == hdr.seqnum && tx.activityuuid == hdr.activityuuid && ((hdr.pkt_type == DCERPC_TYPE_REQUEST && !tx.req_done) || (hdr.pkt_type == DCERPC_TYPE_RESPONSE && !tx.resp_done)) { - SCLogDebug!("found tx id {}, last tx_id {}, {} {}", tx.id, self.tx_id, tx.seqnum, tx.activityuuid[0]); - return Some(tx); - } - } - None + return self.transactions.iter_mut().find(|tx| { + tx.seqnum == hdr.seqnum + && tx.activityuuid == hdr.activityuuid + && ((hdr.pkt_type == DCERPC_TYPE_REQUEST && !tx.req_done) + || (hdr.pkt_type == DCERPC_TYPE_RESPONSE && !tx.resp_done)) + }); } pub fn handle_fragment_data(&mut self, hdr: &DCERPCHdrUdp, input: &[u8]) -> bool { diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index 57f66c0f73df..d4f71d268273 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -358,15 +358,7 @@ impl DNSState { } pub fn get_tx(&mut self, tx_id: u64) -> Option<&DNSTransaction> { - SCLogDebug!("get_tx: tx_id={}", tx_id); - for tx in &mut self.transactions { - if tx.id == tx_id + 1 { - SCLogDebug!("Found DNS TX with ID {}", tx_id); - return Some(tx); - } - } - SCLogDebug!("Failed to find DNS TX with ID {}", tx_id); - return None; + return self.transactions.iter().find(|&tx| tx.id == tx_id + 1); } /// Set an event. The event is set on the most recent transaction. diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index dfb5e0e72445..e14b0114eac9 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -462,27 +462,11 @@ impl NFSState { } pub fn get_tx_by_id(&mut self, tx_id: u64) -> Option<&NFSTransaction> { - SCLogDebug!("get_tx_by_id: tx_id={}", tx_id); - for tx in &mut self.transactions { - if tx.id == tx_id + 1 { - SCLogDebug!("Found NFS TX with ID {}", tx_id); - return Some(tx); - } - } - SCLogDebug!("Failed to find NFS TX with ID {}", tx_id); - return None; + return self.transactions.iter().find(|&tx| tx.id == tx_id + 1); } pub fn get_tx_by_xid(&mut self, tx_xid: u32) -> Option<&mut NFSTransaction> { - SCLogDebug!("get_tx_by_xid: tx_xid={}", tx_xid); - for tx in &mut self.transactions { - if !tx.is_file_tx && tx.xid == tx_xid { - SCLogDebug!("Found NFS TX with ID {} XID {:04X}", tx.id, tx.xid); - return Some(tx); - } - } - SCLogDebug!("Failed to find NFS TX with XID {:04X}", tx_xid); - return None; + return self.transactions.iter_mut().find(|tx| !tx.is_file_tx && tx.xid == tx_xid); } /// Set an event. The event is set on the most recent transaction. @@ -685,15 +669,11 @@ impl NFSState { } pub fn xidmap_handle2name(&mut self, xidmap: &mut NFSRequestXidMap) { - match self.namemap.get(&xidmap.file_handle) { - Some(n) => { - SCLogDebug!("xidmap_handle2name: name {:?}", n); - xidmap.file_name = n.to_vec(); - }, - _ => { - SCLogDebug!("xidmap_handle2name: object {:?} not found", - xidmap.file_handle); - }, + if let Some(n) = self.namemap.get(&xidmap.file_handle) { + SCLogDebug!("xidmap_handle2name: name {:?}", n); + xidmap.file_name = n.to_vec(); + } else { + SCLogDebug!("xidmap_handle2name: object {:?} not found", xidmap.file_handle); } } diff --git a/rust/src/smb/auth.rs b/rust/src/smb/auth.rs index c5d20bba6e29..46f22bf7cffd 100644 --- a/rust/src/smb/auth.rs +++ b/rust/src/smb/auth.rs @@ -105,21 +105,20 @@ fn parse_secblob_spnego(blob: &[u8]) -> Option BerObjectContent::Sequence(ref seq) => { for se in seq { SCLogDebug!("SEQ {:?}", se); - match se.content { - BerObjectContent::OID(ref oid) => { - SCLogDebug!("OID {:?}", oid); - match oid.to_string().as_str() { - "1.2.840.48018.1.2.2" => { SCLogDebug!("Microsoft Kerberos 5"); }, - "1.2.840.113554.1.2.2" => { SCLogDebug!("Kerberos 5"); have_kerberos = true; }, - "1.2.840.113554.1.2.2.1" => { SCLogDebug!("krb5-name"); }, - "1.2.840.113554.1.2.2.2" => { SCLogDebug!("krb5-principal"); }, - "1.2.840.113554.1.2.2.3" => { SCLogDebug!("krb5-user-to-user-mech"); }, - "1.3.6.1.4.1.311.2.2.10" => { SCLogDebug!("NTLMSSP"); have_ntlmssp = true; }, - "1.3.6.1.4.1.311.2.2.30" => { SCLogDebug!("NegoEx"); }, - _ => { SCLogDebug!("unexpected OID {:?}", oid); }, - } - }, - _ => { SCLogDebug!("expected OID, got {:?}", se); }, + if let BerObjectContent::OID(ref oid) = se.content { + SCLogDebug!("OID {:?}", oid); + match oid.to_string().as_str() { + "1.2.840.48018.1.2.2" => { SCLogDebug!("Microsoft Kerberos 5"); }, + "1.2.840.113554.1.2.2" => { SCLogDebug!("Kerberos 5"); have_kerberos = true; }, + "1.2.840.113554.1.2.2.1" => { SCLogDebug!("krb5-name"); }, + "1.2.840.113554.1.2.2.2" => { SCLogDebug!("krb5-principal"); }, + "1.2.840.113554.1.2.2.3" => { SCLogDebug!("krb5-user-to-user-mech"); }, + "1.3.6.1.4.1.311.2.2.10" => { SCLogDebug!("NTLMSSP"); have_ntlmssp = true; }, + "1.3.6.1.4.1.311.2.2.30" => { SCLogDebug!("NegoEx"); }, + _ => { SCLogDebug!("unexpected OID {:?}", oid); }, + } + } else { + SCLogDebug!("expected OID, got {:?}", se); } } }, diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index d6b0a565c060..a34621746349 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -2025,57 +2025,51 @@ fn smb_probe_tcp_midstream(direction: Direction, slice: &[u8], rdir: *mut u8, be } else { search_smb_record(slice) }; - match r { - Ok((_, data)) => { - SCLogDebug!("smb found"); - match parse_smb_version(data) { - Ok((_, ref smb)) => { - SCLogDebug!("SMB {:?}", smb); - if smb.version == 0xff_u8 { // SMB1 - SCLogDebug!("SMBv1 record"); - if let Ok((_, ref smb_record)) = parse_smb_record(data) { - if smb_record.flags & 0x80 != 0 { - SCLogDebug!("RESPONSE {:02x}", smb_record.flags); - if direction == Direction::ToServer { - unsafe { *rdir = Direction::ToClient as u8; } - } - } else { - SCLogDebug!("REQUEST {:02x}", smb_record.flags); - if direction == Direction::ToClient { - unsafe { *rdir = Direction::ToServer as u8; } - } - } - return 1; + if let Ok((_, data)) = r { + SCLogDebug!("smb found"); + if let Ok((_, ref smb)) = parse_smb_version(data) { + SCLogDebug!("SMB {:?}", smb); + if smb.version == 0xff_u8 { // SMB1 + SCLogDebug!("SMBv1 record"); + if let Ok((_, ref smb_record)) = parse_smb_record(data) { + if smb_record.flags & 0x80 != 0 { + SCLogDebug!("RESPONSE {:02x}", smb_record.flags); + if direction == Direction::ToServer { + unsafe { *rdir = Direction::ToClient as u8; } } - } else if smb.version == 0xfe_u8 { // SMB2 - SCLogDebug!("SMB2 record"); - if let Ok((_, ref smb_record)) = parse_smb2_record_direction(data) { - if direction == Direction::ToServer { - SCLogDebug!("direction Direction::ToServer smb_record {:?}", smb_record); - if !smb_record.request { - unsafe { *rdir = Direction::ToClient as u8; } - } - } else { - SCLogDebug!("direction Direction::ToClient smb_record {:?}", smb_record); - if smb_record.request { - unsafe { *rdir = Direction::ToServer as u8; } - } - } + } else { + SCLogDebug!("REQUEST {:02x}", smb_record.flags); + if direction == Direction::ToClient { + unsafe { *rdir = Direction::ToServer as u8; } } } - else if smb.version == 0xfd_u8 { // SMB3 transform - SCLogDebug!("SMB3 record"); - } return 1; - }, - _ => { - SCLogDebug!("smb not found in {:?}", slice); - }, + } + } else if smb.version == 0xfe_u8 { // SMB2 + SCLogDebug!("SMB2 record"); + if let Ok((_, ref smb_record)) = parse_smb2_record_direction(data) { + if direction == Direction::ToServer { + SCLogDebug!("direction Direction::ToServer smb_record {:?}", smb_record); + if !smb_record.request { + unsafe { *rdir = Direction::ToClient as u8; } + } + } else { + SCLogDebug!("direction Direction::ToClient smb_record {:?}", smb_record); + if smb_record.request { + unsafe { *rdir = Direction::ToServer as u8; } + } + } + } + } + else if smb.version == 0xfd_u8 { // SMB3 transform + SCLogDebug!("SMB3 record"); } - }, - _ => { - SCLogDebug!("no dice"); - }, + return 1; + } else { + SCLogDebug!("smb not found in {:?}", slice); + } + } else { + SCLogDebug!("no dice"); } return 0; } diff --git a/rust/src/smb/smb1.rs b/rust/src/smb/smb1.rs index 9d7d47e27c85..a353c5539ccd 100644 --- a/rust/src/smb/smb1.rs +++ b/rust/src/smb/smb1.rs @@ -725,19 +725,16 @@ fn smb1_response_record_one(state: &mut SMBState, r: &SmbRecord, command: u8, an SCLogDebug!("Create AndX {:?}", cr); let guid_key = SMBCommonHdr::from1(r, SMBHDR_TYPE_FILENAME); - match state.ssn2vec_map.remove(&guid_key) { - Some(mut p) => { - p.retain(|&i|i != 0x00); - - let mut fid = cr.fid.to_vec(); - fid.extend_from_slice(&u32_as_bytes(r.ssn_id)); - SCLogDebug!("SMB1_COMMAND_NT_CREATE_ANDX fid {:?}", fid); - SCLogDebug!("fid {:?} name {:?}", fid, p); - state.guid2name_map.insert(fid, p); - }, - _ => { - SCLogDebug!("SMBv1 response: GUID NOT FOUND"); - }, + if let Some(mut p) = state.ssn2vec_map.remove(&guid_key) { + p.retain(|&i|i != 0x00); + + let mut fid = cr.fid.to_vec(); + fid.extend_from_slice(&u32_as_bytes(r.ssn_id)); + SCLogDebug!("SMB1_COMMAND_NT_CREATE_ANDX fid {:?}", fid); + SCLogDebug!("fid {:?} name {:?}", fid, p); + state.guid2name_map.insert(fid, p); + } else { + SCLogDebug!("SMBv1 response: GUID NOT FOUND"); } let tx_hdr = SMBCommonHdr::from1(r, SMBHDR_TYPE_GENERICTX); diff --git a/rust/src/smb/smb1_session.rs b/rust/src/smb/smb1_session.rs index c39c7ce98fc9..80f23c8a64e9 100644 --- a/rust/src/smb/smb1_session.rs +++ b/rust/src/smb/smb1_session.rs @@ -187,16 +187,13 @@ pub fn smb1_session_setup_response(state: &mut SMBState, r: &SmbRecord, andx_off }; // otherwise try match with ssn id 0 (e.g. NTLMSSP_NEGOTIATE) if !found { - match state.get_sessionsetup_tx( + if let Some(tx) = state.get_sessionsetup_tx( SMBCommonHdr::new(SMBHDR_TYPE_HEADER, 0, 0, r.multiplex_id as u64)) { - Some(tx) => { - smb1_session_setup_update_tx(tx, r, andx_offset); - SCLogDebug!("smb1_session_setup_response: tx {:?}", tx); - }, - None => { - SCLogDebug!("smb1_session_setup_response: tx not found for {:?}", r); - }, + smb1_session_setup_update_tx(tx, r, andx_offset); + SCLogDebug!("smb1_session_setup_response: tx {:?}", tx); + } else { + SCLogDebug!("smb1_session_setup_response: tx not found for {:?}", r); } } } diff --git a/rust/src/smb/smb2_session.rs b/rust/src/smb/smb2_session.rs index 93cc99cdd4c6..31a34165e4f2 100644 --- a/rust/src/smb/smb2_session.rs +++ b/rust/src/smb/smb2_session.rs @@ -70,16 +70,13 @@ pub fn smb2_session_setup_response(state: &mut SMBState, r: &Smb2Record) }; // otherwise try match with ssn id 0 (e.g. NTLMSSP_NEGOTIATE) if !found { - match state.get_sessionsetup_tx( + if let Some(tx) = state.get_sessionsetup_tx( SMBCommonHdr::new(SMBHDR_TYPE_HEADER, 0, 0, r.message_id)) { - Some(tx) => { - smb2_session_setup_update_tx(tx, r); - SCLogDebug!("smb2_session_setup_response: tx {:?}", tx); - }, - None => { - SCLogDebug!("smb2_session_setup_response: tx not found for {:?}", r); - }, + smb2_session_setup_update_tx(tx, r); + SCLogDebug!("smb2_session_setup_response: tx {:?}", tx); + } else { + SCLogDebug!("smb2_session_setup_response: tx not found for {:?}", r); } } }