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)
pull/7684/head
Jason Ish 3 years ago committed by Victor Julien
parent f92708b8ca
commit c862e84c01

@ -54,6 +54,15 @@ impl Default for Direction {
fn default() -> Self { Direction::ToServer } 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<u8> for Direction { impl From<u8> for Direction {
fn from(d: u8) -> Self { fn from(d: u8) -> Self {
if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) { if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) {

@ -18,6 +18,7 @@
use crate::applayer::StreamSlice; use crate::applayer::StreamSlice;
use crate::core::Flow; use crate::core::Flow;
use crate::core::STREAM_TOSERVER; use crate::core::STREAM_TOSERVER;
use crate::core::Direction;
#[repr(C)] #[repr(C)]
struct CFrame { struct CFrame {
@ -38,7 +39,7 @@ extern {
pub struct Frame { pub struct Frame {
pub id: i64, pub id: i64,
direction: i32, direction: Direction,
} }
impl std::fmt::Debug for Frame { impl std::fmt::Debug for Frame {
@ -52,8 +53,6 @@ impl Frame {
flow: *const Flow, stream_slice: &StreamSlice, frame_start: &[u8], frame_len: i64, flow: *const Flow, stream_slice: &StreamSlice, frame_start: &[u8], frame_len: i64,
frame_type: u8, frame_type: u8,
) -> Option<Self> { ) -> Option<Self> {
// 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; 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()); 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 // 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, stream_slice,
offset as u32, offset as u32,
frame_len, frame_len,
direction, if stream_slice.flags() & STREAM_TOSERVER != 0 { 0 } else { 1 },
frame_type, frame_type,
) )
}; };
let id = unsafe { AppLayerFrameGetId(frame) }; let id = unsafe { AppLayerFrameGetId(frame) };
if id > 0 { if id > 0 {
Some(Self { id, direction }) Some(Self {
id,
direction: Direction::from(stream_slice.flags()),
})
} else { } else {
None 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) { pub fn set_len(&self, flow: *const Flow, len: i64) {
unsafe { 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) { pub fn set_tx(&self, flow: *const Flow, tx_id: u64) {
unsafe { 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) { pub fn add_event(&self, flow: *const Flow, event: u8) {
unsafe { unsafe {
AppLayerFrameAddEventById(flow, self.direction, self.id, event); AppLayerFrameAddEventById(flow, self.direction(), self.id, event);
}; };
} }
} }

Loading…
Cancel
Save