From: Jason Ish Date: Wed, 29 Jun 2022 17:28:25 +0000 (-0600) Subject: rust/frames: cleanups X-Git-Tag: suricata-7.0.0-beta1~323 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c862e84c01d59b71cd44b57998936c48b4d142b2;p=thirdparty%2Fsuricata.git rust/frames: cleanups - Implement the Display trait on Direction to print "toserver" or "toclient" which used in a format string. - Use Direction struct inside Frame instead of a u32. Requires a helper method as there are two representation in C for direction, and the C methods for frames don't use the internal representation of the Direction enum (some sweeping changes could help here) --- diff --git a/rust/src/core.rs b/rust/src/core.rs index 8c3fee58cb..197aa54fb0 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -54,6 +54,15 @@ impl Default for Direction { fn default() -> Self { Direction::ToServer } } +impl std::fmt::Display for Direction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::ToServer => write!(f, "toserver"), + Self::ToClient => write!(f, "toclient"), + } + } +} + impl From for Direction { fn from(d: u8) -> Self { if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) { diff --git a/rust/src/frames.rs b/rust/src/frames.rs index 7f75878489..3f1953d5d2 100644 --- a/rust/src/frames.rs +++ b/rust/src/frames.rs @@ -18,6 +18,7 @@ use crate::applayer::StreamSlice; use crate::core::Flow; use crate::core::STREAM_TOSERVER; +use crate::core::Direction; #[repr(C)] struct CFrame { @@ -38,7 +39,7 @@ extern { pub struct Frame { pub id: i64, - direction: i32, + direction: Direction, } impl std::fmt::Debug for Frame { @@ -52,8 +53,6 @@ impl Frame { flow: *const Flow, stream_slice: &StreamSlice, frame_start: &[u8], frame_len: i64, frame_type: u8, ) -> Option { - // Derive the direction from the stream slice, 0 for to server, 1 for to client. - let direction = if stream_slice.flags() & STREAM_TOSERVER != 0 { 0 } else { 1 }; let offset = frame_start.as_ptr() as usize - stream_slice.as_slice().as_ptr() as usize; SCLogDebug!("offset {} stream_slice.len() {} frame_start.len() {}", offset, stream_slice.len(), frame_start.len()); // If running Rust unit tests this won't be compiled and None will be returned, as we don't @@ -65,13 +64,16 @@ impl Frame { stream_slice, offset as u32, frame_len, - direction, + if stream_slice.flags() & STREAM_TOSERVER != 0 { 0 } else { 1 }, frame_type, ) }; let id = unsafe { AppLayerFrameGetId(frame) }; if id > 0 { - Some(Self { id, direction }) + Some(Self { + id, + direction: Direction::from(stream_slice.flags()), + }) } else { None } @@ -80,21 +82,31 @@ impl Frame { } } + /// Conversion function to get the direction in the correct form for the + /// C frame methods which takes direction as a u32 value of 0 or 1 rather + /// than the flag value used internally by Frame. + fn direction(&self) -> i32 { + match self.direction { + Direction::ToServer => 0, + Direction::ToClient => 1, + } + } + pub fn set_len(&self, flow: *const Flow, len: i64) { unsafe { - AppLayerFrameSetLengthById(flow, self.direction, self.id, len); + AppLayerFrameSetLengthById(flow, self.direction(), self.id, len); }; } pub fn set_tx(&self, flow: *const Flow, tx_id: u64) { unsafe { - AppLayerFrameSetTxIdById(flow, self.direction, self.id, tx_id); + AppLayerFrameSetTxIdById(flow, self.direction(), self.id, tx_id); }; } pub fn add_event(&self, flow: *const Flow, event: u8) { unsafe { - AppLayerFrameAddEventById(flow, self.direction, self.id, event); + AppLayerFrameAddEventById(flow, self.direction(), self.id, event); }; } }