]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
dns: set tx id for frames
authorPhilippe Antoine <pantoine@oisf.net>
Fri, 26 Apr 2024 19:05:36 +0000 (21:05 +0200)
committerVictor Julien <victor@inliniac.net>
Tue, 14 May 2024 16:00:20 +0000 (18:00 +0200)
rust/src/dns/dns.rs
rust/src/frames.rs

index 4aac9ed90e4275c608f090f3ca3e7ecf78990f6e..587054dab6ee9c5c65d33f3ab4a686e23fe75786 100644 (file)
@@ -381,7 +381,7 @@ impl DNSState {
         None
     }
 
-    fn parse_request(&mut self, input: &[u8], is_tcp: bool) -> bool {
+    fn parse_request(&mut self, input: &[u8], is_tcp: bool, frame: Option<Frame>, flow: *const core::Flow,) -> bool {
         let (body, header) = if let Some((body, header)) = self.validate_header(input) {
             (body, header)
         } else {
@@ -400,6 +400,9 @@ impl DNSState {
                 let opcode = ((request.header.flags >> 11) & 0xf) as u8;
 
                 let mut tx = self.new_tx(Direction::ToServer);
+                if let Some(frame) = frame {
+                    frame.set_tx(flow, tx.id);
+                }
                 tx.request = Some(request);
                 self.transactions.push_back(tx);
 
@@ -431,7 +434,7 @@ impl DNSState {
 
     fn parse_request_udp(&mut self, flow: *const core::Flow, stream_slice: StreamSlice) -> bool {
         let input = stream_slice.as_slice();
-        let _pdu = Frame::new(
+        let frame = Frame::new(
             flow,
             &stream_slice,
             input,
@@ -439,12 +442,12 @@ impl DNSState {
             DnsFrameType::Pdu as u8,
             None,
         );
-        self.parse_request(input, false)
+        self.parse_request(input, false, frame, flow)
     }
 
     fn parse_response_udp(&mut self, flow: *const core::Flow, stream_slice: StreamSlice) -> bool {
         let input = stream_slice.as_slice();
-        let _pdu = Frame::new(
+        let frame = Frame::new(
             flow,
             &stream_slice,
             input,
@@ -452,10 +455,10 @@ impl DNSState {
             DnsFrameType::Pdu as u8,
             None,
         );
-        self.parse_response(input, false)
+        self.parse_response(input, false, frame, flow)
     }
 
-    fn parse_response(&mut self, input: &[u8], is_tcp: bool) -> bool {
+    fn parse_response(&mut self, input: &[u8], is_tcp: bool, frame: Option<Frame>, flow: *const core::Flow) -> bool {
         let (body, header) = if let Some((body, header)) = self.validate_header(input) {
             (body, header)
         } else {
@@ -475,6 +478,9 @@ impl DNSState {
                 let opcode = ((response.header.flags >> 11) & 0xf) as u8;
 
                 let mut tx = self.new_tx(Direction::ToClient);
+                if let Some(frame) = frame {
+                    frame.set_tx(flow, tx.id);
+                }
                 if let Some(ref mut config) = &mut self.config {
                     if let Some(config) = config.remove(&response.header.tx_id) {
                         tx.tx_data.config = config;
@@ -543,7 +549,7 @@ impl DNSState {
             );
             if size > 0 && cur_i.len() >= size + 2 {
                 let msg = &cur_i[2..(size + 2)];
-                let _pdu = Frame::new(
+                let frame = Frame::new(
                     flow,
                     &stream_slice,
                     msg,
@@ -551,7 +557,7 @@ impl DNSState {
                     DnsFrameType::Pdu as u8,
                     None,
                 );
-                if self.parse_request(msg, true) {
+                if self.parse_request(msg, true, frame, flow) {
                     cur_i = &cur_i[(size + 2)..];
                     consumed += size + 2;
                 } else {
@@ -606,7 +612,7 @@ impl DNSState {
             );
             if size > 0 && cur_i.len() >= size + 2 {
                 let msg = &cur_i[2..(size + 2)];
-                let _pdu = Frame::new(
+                let frame = Frame::new(
                     flow,
                     &stream_slice,
                     msg,
@@ -614,7 +620,7 @@ impl DNSState {
                     DnsFrameType::Pdu as u8,
                     None,
                 );
-                if self.parse_response(msg, true) {
+                if self.parse_response(msg, true, frame, flow) {
                     cur_i = &cur_i[(size + 2)..];
                     consumed += size + 2;
                 } else {
@@ -1237,7 +1243,7 @@ mod tests {
             0x80,
         ];
         let mut state = DNSState::new();
-        assert!(state.parse_response(buf, false));
+        assert!(state.parse_response(buf, false, None, std::ptr::null()));
     }
 
     // Port of the C RustDNSUDPParserTest02 unit test.
@@ -1257,7 +1263,7 @@ mod tests {
             0x10,0x00,0x02,0xC0,0x85,0x00,0x00,0x29,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
         ];
         let mut state = DNSState::new();
-        assert!(state.parse_response(buf, false));
+        assert!(state.parse_response(buf, false, None, std::ptr::null()));
     }
 
     // Port of the C RustDNSUDPParserTest03 unit test.
@@ -1277,7 +1283,7 @@ mod tests {
             0x29,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00
         ];
         let mut state = DNSState::new();
-        assert!(state.parse_response(buf, false));
+        assert!(state.parse_response(buf, false, None, std::ptr::null()));
     }
 
     // Port of the C RustDNSUDPParserTest04 unit test.
@@ -1301,7 +1307,7 @@ mod tests {
             0x6b,0x00,0x01,0x00,0x01,0x00,0x09,0x3a,0x80,0x00,0x04,0x0a,0x1e,0x1c,0x5f
         ];
         let mut state = DNSState::new();
-        assert!(state.parse_response(buf, false));
+        assert!(state.parse_response(buf, false, None, std::ptr::null()));
     }
 
     // Port of the C RustDNSUDPParserTest05 unit test.
@@ -1325,7 +1331,7 @@ mod tests {
             0x6b,0x00,0x01,0x00,0x01,0x00,0x09,0x3a,0x80,0x00,0x04,0x0a,0x1e,0x1c,0x5f
         ];
         let mut state = DNSState::new();
-        assert!(!state.parse_response(buf, false));
+        assert!(!state.parse_response(buf, false, None, std::ptr::null()));
     }
 
     // Port of the C RustDNSTCPParserTestMultiRecord unit test.
index de1ee0e79c2d921fb66dfee313ce9bafbaef8420..1d8ce40ef16d32b158c2ea27de2180a90569601f 100644 (file)
@@ -38,6 +38,7 @@ extern {
     ) -> *const CFrame;
     fn AppLayerFrameAddEventById(flow: *const Flow, dir: i32, id: i64, event: u8);
     fn AppLayerFrameSetLengthById(flow: *const Flow, dir: i32, id: i64, len: i64);
+    #[cfg(not(test))]
     fn AppLayerFrameSetTxIdById(flow: *const Flow, dir: i32, id: i64, tx_id: u64);
     #[cfg(not(test))]
     fn AppLayerFrameGetId(frame: *const CFrame) -> i64;
@@ -118,6 +119,7 @@ impl Frame {
         };
     }
 
+    #[cfg(not(test))]
     #[allow(clippy::not_unsafe_ptr_arg_deref)]
     pub fn set_tx(&self, flow: *const Flow, tx_id: u64) {
         unsafe {
@@ -125,6 +127,12 @@ impl Frame {
         };
     }
 
+    /// A variation of `set_tx` for use when running Rust unit tests as
+    /// the C functions for building a frame are not available for
+    /// linkage.
+    #[cfg(test)]
+    pub fn set_tx(&self, _flow: *const Flow, _tx_id: u64) {}
+
     #[allow(clippy::not_unsafe_ptr_arg_deref)]
     pub fn add_event(&self, flow: *const Flow, event: u8) {
         unsafe {