Skip to content

Commit 38db51b

Browse files
catenacybervictorjulien
authored andcommitted
rust: make cargo clippy clean
Fixing single_match and manual_find intertwined with SCLogDebug
1 parent 89936b6 commit 38db51b

File tree

8 files changed

+88
-133
lines changed

8 files changed

+88
-133
lines changed

rust/src/dcerpc/dcerpc_udp.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ impl DCERPCUDPState {
141141
}
142142

143143
fn find_incomplete_tx(&mut self, hdr: &DCERPCHdrUdp) -> Option<&mut DCERPCTransaction> {
144-
for tx in &mut self.transactions {
145-
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)) {
146-
SCLogDebug!("found tx id {}, last tx_id {}, {} {}", tx.id, self.tx_id, tx.seqnum, tx.activityuuid[0]);
147-
return Some(tx);
148-
}
149-
}
150-
None
144+
return self.transactions.iter_mut().find(|tx| {
145+
tx.seqnum == hdr.seqnum
146+
&& tx.activityuuid == hdr.activityuuid
147+
&& ((hdr.pkt_type == DCERPC_TYPE_REQUEST && !tx.req_done)
148+
|| (hdr.pkt_type == DCERPC_TYPE_RESPONSE && !tx.resp_done))
149+
});
151150
}
152151

153152
pub fn handle_fragment_data(&mut self, hdr: &DCERPCHdrUdp, input: &[u8]) -> bool {

rust/src/dns/dns.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,7 @@ impl DNSState {
358358
}
359359

360360
pub fn get_tx(&mut self, tx_id: u64) -> Option<&DNSTransaction> {
361-
SCLogDebug!("get_tx: tx_id={}", tx_id);
362-
for tx in &mut self.transactions {
363-
if tx.id == tx_id + 1 {
364-
SCLogDebug!("Found DNS TX with ID {}", tx_id);
365-
return Some(tx);
366-
}
367-
}
368-
SCLogDebug!("Failed to find DNS TX with ID {}", tx_id);
369-
return None;
361+
return self.transactions.iter().find(|&tx| tx.id == tx_id + 1);
370362
}
371363

372364
/// Set an event. The event is set on the most recent transaction.

rust/src/nfs/nfs.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -462,27 +462,11 @@ impl NFSState {
462462
}
463463

464464
pub fn get_tx_by_id(&mut self, tx_id: u64) -> Option<&NFSTransaction> {
465-
SCLogDebug!("get_tx_by_id: tx_id={}", tx_id);
466-
for tx in &mut self.transactions {
467-
if tx.id == tx_id + 1 {
468-
SCLogDebug!("Found NFS TX with ID {}", tx_id);
469-
return Some(tx);
470-
}
471-
}
472-
SCLogDebug!("Failed to find NFS TX with ID {}", tx_id);
473-
return None;
465+
return self.transactions.iter().find(|&tx| tx.id == tx_id + 1);
474466
}
475467

476468
pub fn get_tx_by_xid(&mut self, tx_xid: u32) -> Option<&mut NFSTransaction> {
477-
SCLogDebug!("get_tx_by_xid: tx_xid={}", tx_xid);
478-
for tx in &mut self.transactions {
479-
if !tx.is_file_tx && tx.xid == tx_xid {
480-
SCLogDebug!("Found NFS TX with ID {} XID {:04X}", tx.id, tx.xid);
481-
return Some(tx);
482-
}
483-
}
484-
SCLogDebug!("Failed to find NFS TX with XID {:04X}", tx_xid);
485-
return None;
469+
return self.transactions.iter_mut().find(|tx| !tx.is_file_tx && tx.xid == tx_xid);
486470
}
487471

488472
/// Set an event. The event is set on the most recent transaction.
@@ -685,15 +669,11 @@ impl NFSState {
685669
}
686670

687671
pub fn xidmap_handle2name(&mut self, xidmap: &mut NFSRequestXidMap) {
688-
match self.namemap.get(&xidmap.file_handle) {
689-
Some(n) => {
690-
SCLogDebug!("xidmap_handle2name: name {:?}", n);
691-
xidmap.file_name = n.to_vec();
692-
},
693-
_ => {
694-
SCLogDebug!("xidmap_handle2name: object {:?} not found",
695-
xidmap.file_handle);
696-
},
672+
if let Some(n) = self.namemap.get(&xidmap.file_handle) {
673+
SCLogDebug!("xidmap_handle2name: name {:?}", n);
674+
xidmap.file_name = n.to_vec();
675+
} else {
676+
SCLogDebug!("xidmap_handle2name: object {:?} not found", xidmap.file_handle);
697677
}
698678
}
699679

rust/src/smb/auth.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,20 @@ fn parse_secblob_spnego(blob: &[u8]) -> Option<SpnegoRequest>
105105
BerObjectContent::Sequence(ref seq) => {
106106
for se in seq {
107107
SCLogDebug!("SEQ {:?}", se);
108-
match se.content {
109-
BerObjectContent::OID(ref oid) => {
110-
SCLogDebug!("OID {:?}", oid);
111-
match oid.to_string().as_str() {
112-
"1.2.840.48018.1.2.2" => { SCLogDebug!("Microsoft Kerberos 5"); },
113-
"1.2.840.113554.1.2.2" => { SCLogDebug!("Kerberos 5"); have_kerberos = true; },
114-
"1.2.840.113554.1.2.2.1" => { SCLogDebug!("krb5-name"); },
115-
"1.2.840.113554.1.2.2.2" => { SCLogDebug!("krb5-principal"); },
116-
"1.2.840.113554.1.2.2.3" => { SCLogDebug!("krb5-user-to-user-mech"); },
117-
"1.3.6.1.4.1.311.2.2.10" => { SCLogDebug!("NTLMSSP"); have_ntlmssp = true; },
118-
"1.3.6.1.4.1.311.2.2.30" => { SCLogDebug!("NegoEx"); },
119-
_ => { SCLogDebug!("unexpected OID {:?}", oid); },
120-
}
121-
},
122-
_ => { SCLogDebug!("expected OID, got {:?}", se); },
108+
if let BerObjectContent::OID(ref oid) = se.content {
109+
SCLogDebug!("OID {:?}", oid);
110+
match oid.to_string().as_str() {
111+
"1.2.840.48018.1.2.2" => { SCLogDebug!("Microsoft Kerberos 5"); },
112+
"1.2.840.113554.1.2.2" => { SCLogDebug!("Kerberos 5"); have_kerberos = true; },
113+
"1.2.840.113554.1.2.2.1" => { SCLogDebug!("krb5-name"); },
114+
"1.2.840.113554.1.2.2.2" => { SCLogDebug!("krb5-principal"); },
115+
"1.2.840.113554.1.2.2.3" => { SCLogDebug!("krb5-user-to-user-mech"); },
116+
"1.3.6.1.4.1.311.2.2.10" => { SCLogDebug!("NTLMSSP"); have_ntlmssp = true; },
117+
"1.3.6.1.4.1.311.2.2.30" => { SCLogDebug!("NegoEx"); },
118+
_ => { SCLogDebug!("unexpected OID {:?}", oid); },
119+
}
120+
} else {
121+
SCLogDebug!("expected OID, got {:?}", se);
123122
}
124123
}
125124
},

rust/src/smb/smb.rs

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,57 +2025,51 @@ fn smb_probe_tcp_midstream(direction: Direction, slice: &[u8], rdir: *mut u8, be
20252025
} else {
20262026
search_smb_record(slice)
20272027
};
2028-
match r {
2029-
Ok((_, data)) => {
2030-
SCLogDebug!("smb found");
2031-
match parse_smb_version(data) {
2032-
Ok((_, ref smb)) => {
2033-
SCLogDebug!("SMB {:?}", smb);
2034-
if smb.version == 0xff_u8 { // SMB1
2035-
SCLogDebug!("SMBv1 record");
2036-
if let Ok((_, ref smb_record)) = parse_smb_record(data) {
2037-
if smb_record.flags & 0x80 != 0 {
2038-
SCLogDebug!("RESPONSE {:02x}", smb_record.flags);
2039-
if direction == Direction::ToServer {
2040-
unsafe { *rdir = Direction::ToClient as u8; }
2041-
}
2042-
} else {
2043-
SCLogDebug!("REQUEST {:02x}", smb_record.flags);
2044-
if direction == Direction::ToClient {
2045-
unsafe { *rdir = Direction::ToServer as u8; }
2046-
}
2047-
}
2048-
return 1;
2028+
if let Ok((_, data)) = r {
2029+
SCLogDebug!("smb found");
2030+
if let Ok((_, ref smb)) = parse_smb_version(data) {
2031+
SCLogDebug!("SMB {:?}", smb);
2032+
if smb.version == 0xff_u8 { // SMB1
2033+
SCLogDebug!("SMBv1 record");
2034+
if let Ok((_, ref smb_record)) = parse_smb_record(data) {
2035+
if smb_record.flags & 0x80 != 0 {
2036+
SCLogDebug!("RESPONSE {:02x}", smb_record.flags);
2037+
if direction == Direction::ToServer {
2038+
unsafe { *rdir = Direction::ToClient as u8; }
20492039
}
2050-
} else if smb.version == 0xfe_u8 { // SMB2
2051-
SCLogDebug!("SMB2 record");
2052-
if let Ok((_, ref smb_record)) = parse_smb2_record_direction(data) {
2053-
if direction == Direction::ToServer {
2054-
SCLogDebug!("direction Direction::ToServer smb_record {:?}", smb_record);
2055-
if !smb_record.request {
2056-
unsafe { *rdir = Direction::ToClient as u8; }
2057-
}
2058-
} else {
2059-
SCLogDebug!("direction Direction::ToClient smb_record {:?}", smb_record);
2060-
if smb_record.request {
2061-
unsafe { *rdir = Direction::ToServer as u8; }
2062-
}
2063-
}
2040+
} else {
2041+
SCLogDebug!("REQUEST {:02x}", smb_record.flags);
2042+
if direction == Direction::ToClient {
2043+
unsafe { *rdir = Direction::ToServer as u8; }
20642044
}
20652045
}
2066-
else if smb.version == 0xfd_u8 { // SMB3 transform
2067-
SCLogDebug!("SMB3 record");
2068-
}
20692046
return 1;
2070-
},
2071-
_ => {
2072-
SCLogDebug!("smb not found in {:?}", slice);
2073-
},
2047+
}
2048+
} else if smb.version == 0xfe_u8 { // SMB2
2049+
SCLogDebug!("SMB2 record");
2050+
if let Ok((_, ref smb_record)) = parse_smb2_record_direction(data) {
2051+
if direction == Direction::ToServer {
2052+
SCLogDebug!("direction Direction::ToServer smb_record {:?}", smb_record);
2053+
if !smb_record.request {
2054+
unsafe { *rdir = Direction::ToClient as u8; }
2055+
}
2056+
} else {
2057+
SCLogDebug!("direction Direction::ToClient smb_record {:?}", smb_record);
2058+
if smb_record.request {
2059+
unsafe { *rdir = Direction::ToServer as u8; }
2060+
}
2061+
}
2062+
}
2063+
}
2064+
else if smb.version == 0xfd_u8 { // SMB3 transform
2065+
SCLogDebug!("SMB3 record");
20742066
}
2075-
},
2076-
_ => {
2077-
SCLogDebug!("no dice");
2078-
},
2067+
return 1;
2068+
} else {
2069+
SCLogDebug!("smb not found in {:?}", slice);
2070+
}
2071+
} else {
2072+
SCLogDebug!("no dice");
20792073
}
20802074
return 0;
20812075
}

rust/src/smb/smb1.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -725,19 +725,16 @@ fn smb1_response_record_one(state: &mut SMBState, r: &SmbRecord, command: u8, an
725725
SCLogDebug!("Create AndX {:?}", cr);
726726

727727
let guid_key = SMBCommonHdr::from1(r, SMBHDR_TYPE_FILENAME);
728-
match state.ssn2vec_map.remove(&guid_key) {
729-
Some(mut p) => {
730-
p.retain(|&i|i != 0x00);
731-
732-
let mut fid = cr.fid.to_vec();
733-
fid.extend_from_slice(&u32_as_bytes(r.ssn_id));
734-
SCLogDebug!("SMB1_COMMAND_NT_CREATE_ANDX fid {:?}", fid);
735-
SCLogDebug!("fid {:?} name {:?}", fid, p);
736-
state.guid2name_map.insert(fid, p);
737-
},
738-
_ => {
739-
SCLogDebug!("SMBv1 response: GUID NOT FOUND");
740-
},
728+
if let Some(mut p) = state.ssn2vec_map.remove(&guid_key) {
729+
p.retain(|&i|i != 0x00);
730+
731+
let mut fid = cr.fid.to_vec();
732+
fid.extend_from_slice(&u32_as_bytes(r.ssn_id));
733+
SCLogDebug!("SMB1_COMMAND_NT_CREATE_ANDX fid {:?}", fid);
734+
SCLogDebug!("fid {:?} name {:?}", fid, p);
735+
state.guid2name_map.insert(fid, p);
736+
} else {
737+
SCLogDebug!("SMBv1 response: GUID NOT FOUND");
741738
}
742739

743740
let tx_hdr = SMBCommonHdr::from1(r, SMBHDR_TYPE_GENERICTX);

rust/src/smb/smb1_session.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,13 @@ pub fn smb1_session_setup_response(state: &mut SMBState, r: &SmbRecord, andx_off
187187
};
188188
// otherwise try match with ssn id 0 (e.g. NTLMSSP_NEGOTIATE)
189189
if !found {
190-
match state.get_sessionsetup_tx(
190+
if let Some(tx) = state.get_sessionsetup_tx(
191191
SMBCommonHdr::new(SMBHDR_TYPE_HEADER, 0, 0, r.multiplex_id as u64))
192192
{
193-
Some(tx) => {
194-
smb1_session_setup_update_tx(tx, r, andx_offset);
195-
SCLogDebug!("smb1_session_setup_response: tx {:?}", tx);
196-
},
197-
None => {
198-
SCLogDebug!("smb1_session_setup_response: tx not found for {:?}", r);
199-
},
193+
smb1_session_setup_update_tx(tx, r, andx_offset);
194+
SCLogDebug!("smb1_session_setup_response: tx {:?}", tx);
195+
} else {
196+
SCLogDebug!("smb1_session_setup_response: tx not found for {:?}", r);
200197
}
201198
}
202199
}

rust/src/smb/smb2_session.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,13 @@ pub fn smb2_session_setup_response(state: &mut SMBState, r: &Smb2Record)
7070
};
7171
// otherwise try match with ssn id 0 (e.g. NTLMSSP_NEGOTIATE)
7272
if !found {
73-
match state.get_sessionsetup_tx(
73+
if let Some(tx) = state.get_sessionsetup_tx(
7474
SMBCommonHdr::new(SMBHDR_TYPE_HEADER, 0, 0, r.message_id))
7575
{
76-
Some(tx) => {
77-
smb2_session_setup_update_tx(tx, r);
78-
SCLogDebug!("smb2_session_setup_response: tx {:?}", tx);
79-
},
80-
None => {
81-
SCLogDebug!("smb2_session_setup_response: tx not found for {:?}", r);
82-
},
76+
smb2_session_setup_update_tx(tx, r);
77+
SCLogDebug!("smb2_session_setup_response: tx {:?}", tx);
78+
} else {
79+
SCLogDebug!("smb2_session_setup_response: tx not found for {:?}", r);
8380
}
8481
}
8582
}

0 commit comments

Comments
 (0)