]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: make cargo clippy clean
authorPhilippe Antoine <pantoine@oisf.net>
Tue, 16 Jan 2024 10:47:30 +0000 (11:47 +0100)
committerPhilippe Antoine <pantoine@oisf.net>
Fri, 27 Jun 2025 07:41:44 +0000 (09:41 +0200)
Fixing single_match and manual_find intertwined with SCLogDebug

(cherry picked from commit 38db51b8789e9f930ff093f592b5763edb190f43)

rust/src/dcerpc/dcerpc_udp.rs
rust/src/dns/dns.rs
rust/src/nfs/nfs.rs
rust/src/smb/auth.rs
rust/src/smb/smb.rs
rust/src/smb/smb1.rs
rust/src/smb/smb1_session.rs
rust/src/smb/smb2_session.rs

index 785aa2ce3e8d1371aa4cc3f574a46f671ae0ca76..d70ca1b531177f2fa019287966166919accf349c 100644 (file)
@@ -142,13 +142,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 {
index 1550b1deb1e2334ece56a30a53d73762b57720e3..1c8df1bf25db282f0dc834dd6b482d930d34dadf 100644 (file)
@@ -385,15 +385,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.
index 98e6f590578782e10de88016a09024d9a851ac7d..323e1d800491186a4c91eb8691368892750586af 100644 (file)
@@ -464,27 +464,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.
@@ -689,15 +673,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);
         }
     }
 
index c5d20bba6e290f48bca168ee6ee9efeed817029e..46f22bf7cffd6643e804fcbefd358371a58c76cf 100644 (file)
@@ -105,21 +105,20 @@ fn parse_secblob_spnego(blob: &[u8]) -> Option<SpnegoRequest>
             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);
                     }
                 }
             },
index 97d4bf4f22722b0f9fa701533d3360acba924aa2..7ed84b66c2e2c5772d8d933d7b7780b788e3c019 100644 (file)
@@ -2036,57 +2036,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;
 }
index eb2712391150a113effaaca51232928d27ef74c8..b0d4e4d3d9c260513407564555477cbfb1a487e8 100644 (file)
@@ -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);
index c39c7ce98fc9b0c119c7976b038a57554efe53f6..80f23c8a64e929f18563fda9f50583dd2aa610e1 100644 (file)
@@ -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);
         }
     }
 }
index 93cc99cdd4c65b7a42cbfde004a4f80ed0d127e6..31a34165e4f2e6a67a60f18bd44212140f373261 100644 (file)
@@ -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);
         }
     }
 }