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 {
fn from(d: u8) -> Self {
if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) {
use crate::applayer::StreamSlice;
use crate::core::Flow;
use crate::core::STREAM_TOSERVER;
+use crate::core::Direction;
#[repr(C)]
struct CFrame {
pub struct Frame {
pub id: i64,
- direction: i32,
+ direction: Direction,
}
impl std::fmt::Debug for Frame {
flow: *const Flow, stream_slice: &StreamSlice, frame_start: &[u8], frame_len: i64,
frame_type: u8,
) -> 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;
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
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
}
}
}
+ /// 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);
};
}
}