From: Philippe Antoine Date: Fri, 26 Apr 2024 19:05:36 +0000 (+0200) Subject: dns: set tx id for frames X-Git-Tag: suricata-8.0.0-beta1~1329 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b103ae7552d65b034e38fbe387d000f1db71ecd;p=thirdparty%2Fsuricata.git dns: set tx id for frames --- diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index 4aac9ed90e..587054dab6 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -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, 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, 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. diff --git a/rust/src/frames.rs b/rust/src/frames.rs index de1ee0e79c..1d8ce40ef1 100644 --- a/rust/src/frames.rs +++ b/rust/src/frames.rs @@ -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 {