]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/frames: cleanups
authorJason Ish <jason.ish@oisf.net>
Wed, 29 Jun 2022 17:28:25 +0000 (11:28 -0600)
committerVictor Julien <vjulien@oisf.net>
Tue, 2 Aug 2022 20:10:35 +0000 (22:10 +0200)
- 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)

rust/src/core.rs
rust/src/frames.rs

index 8c3fee58cb086eb0c307a04e13c3b67939105da5..197aa54fb0fbf4f73ad75eb7e2e45edf92fd8a92 100644 (file)
@@ -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<u8> for Direction {
     fn from(d: u8) -> Self {
         if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) {
index 7f7587848996bc7fa3dfb223b46fb17e0db1a7f2..3f1953d5d2fef5f933a432f6ac962691cacd8a10 100644 (file)
@@ -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<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
@@ -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);
         };
     }
 }