diff --git a/rust/src/core.rs b/rust/src/core.rs index efb891efdc..3b25ef20c6 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -69,18 +69,6 @@ macro_rules!BIT_U64 { // pub enum StreamingBufferConfig {} -// Opaque flow type (defined in C) -pub enum HttpRangeContainerBlock {} - -pub type SCHttpRangeFreeBlock = extern "C" fn ( - c: *mut HttpRangeContainerBlock); -pub type SCHTPFileCloseHandleRange = extern "C" fn ( - sbcfg: &StreamingBufferConfig, - fc: *mut FileContainer, - flags: u16, - c: *mut HttpRangeContainerBlock, - data: *const u8, - data_len: u32) -> bool; pub type SCFileOpenFileWithId = extern "C" fn ( file_container: &FileContainer, sbcfg: &StreamingBufferConfig, @@ -118,9 +106,6 @@ pub type SCFileContainerRecycle = extern "C" fn ( #[allow(non_snake_case)] #[repr(C)] pub struct SuricataContext { - pub HttpRangeFreeBlock: SCHttpRangeFreeBlock, - pub HTPFileCloseHandleRange: SCHTPFileCloseHandleRange, - pub FileOpenFile: SCFileOpenFileWithId, pub FileCloseFile: SCFileCloseFileById, pub FileAppendData: SCFileAppendDataById, diff --git a/rust/src/http2/http2.rs b/rust/src/http2/http2.rs index d2ccdfb512..d3db7bfc7e 100644 --- a/rust/src/http2/http2.rs +++ b/rust/src/http2/http2.rs @@ -20,6 +20,7 @@ use super::detect; use super::parser; use super::range; +use super::range::{HttpRangeContainerBlock, SCHTPFileCloseHandleRange, SCHttpRangeFreeBlock}; use crate::applayer::{self, *}; use crate::conf::conf_get; use crate::core::*; @@ -217,10 +218,10 @@ impl HTTP2Transaction { pub fn free(&mut self) { if !self.file_range.is_null() { - if let Some(c) = unsafe { SC } { - if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { - //TODO get a file container instead of NULL - (c.HTPFileCloseHandleRange)( + if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { + //TODO get a file container instead of NULL + unsafe { + SCHTPFileCloseHandleRange( sfcm.files_sbcfg, std::ptr::null_mut(), 0, @@ -228,9 +229,9 @@ impl HTTP2Transaction { std::ptr::null_mut(), 0, ); - (c.HttpRangeFreeBlock)(self.file_range); - self.file_range = std::ptr::null_mut(); + SCHttpRangeFreeBlock(self.file_range); } + self.file_range = std::ptr::null_mut(); } } } @@ -614,9 +615,9 @@ impl HTTP2State { // but we need state's file container cf https://redmine.openinfosecfoundation.org/issues/4444 for tx in &mut self.transactions { if !tx.file_range.is_null() { - if let Some(c) = unsafe { SC } { - if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { - (c.HTPFileCloseHandleRange)( + if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { + unsafe { + SCHTPFileCloseHandleRange( sfcm.files_sbcfg, &mut tx.ft_tc.file, 0, @@ -624,9 +625,9 @@ impl HTTP2State { std::ptr::null_mut(), 0, ); - (c.HttpRangeFreeBlock)(tx.file_range); - tx.file_range = std::ptr::null_mut(); + SCHttpRangeFreeBlock(tx.file_range); } + tx.file_range = std::ptr::null_mut(); } } } @@ -655,9 +656,9 @@ impl HTTP2State { // this should be in HTTP2Transaction::free // but we need state's file container cf https://redmine.openinfosecfoundation.org/issues/4444 if !tx.file_range.is_null() { - if let Some(c) = unsafe { SC } { - if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { - (c.HTPFileCloseHandleRange)( + if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { + unsafe { + SCHTPFileCloseHandleRange( sfcm.files_sbcfg, &mut tx.ft_tc.file, 0, @@ -665,9 +666,9 @@ impl HTTP2State { std::ptr::null_mut(), 0, ); - (c.HttpRangeFreeBlock)(tx.file_range); - tx.file_range = std::ptr::null_mut(); + SCHttpRangeFreeBlock(tx.file_range); } + tx.file_range = std::ptr::null_mut(); } } break; diff --git a/rust/src/http2/range.rs b/rust/src/http2/range.rs index 7002572ef8..006d82c225 100644 --- a/rust/src/http2/range.rs +++ b/rust/src/http2/range.rs @@ -16,10 +16,9 @@ */ use super::detect; -use crate::core::{ - HttpRangeContainerBlock, StreamingBufferConfig, SuricataFileContext, SC, -}; +use crate::core::{StreamingBufferConfig, SuricataFileContext}; use crate::direction::Direction; +use crate::filecontainer::FileContainer; use crate::flow::Flow; use crate::http2::http2::HTTP2Transaction; use crate::http2::http2::SURICATA_HTTP2_FILE_CONFIG; @@ -33,6 +32,38 @@ use nom7::{Err, IResult}; use std::os::raw::c_uchar; use std::str::FromStr; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct HttpRangeContainerBlock { + _unused: [u8; 0], +} + +// Defined in app-layer-htp-range.h +#[allow(unused_doc_comments)] +/// cbindgen:ignore +extern "C" { + #[cfg(not(test))] + pub fn SCHttpRangeFreeBlock(range: *mut HttpRangeContainerBlock); + #[cfg(not(test))] + pub fn SCHTPFileCloseHandleRange( + sbcfg: &StreamingBufferConfig, fc: *mut FileContainer, flags: u16, + c: *mut HttpRangeContainerBlock, data: *const u8, data_len: u32, + ) -> bool; +} + +#[cfg(test)] +#[allow(non_snake_case)] +pub(super) unsafe fn SCHttpRangeFreeBlock(_range: *mut HttpRangeContainerBlock) {} + +#[cfg(test)] +#[allow(non_snake_case)] +pub(super) unsafe fn SCHTPFileCloseHandleRange( + _sbcfg: &StreamingBufferConfig, _fc: *mut FileContainer, _flags: u16, + _c: *mut HttpRangeContainerBlock, _data: *const u8, _data_len: u32, +) -> bool { + true +} + #[derive(Debug)] #[repr(C)] pub struct HTTPContentRange { @@ -166,14 +197,14 @@ pub fn http2_range_append( } pub fn http2_range_close(tx: &mut HTTP2Transaction, dir: Direction, data: &[u8]) { - let added = if let Some(c) = unsafe { SC } { - if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { - let (files, flags) = if dir == Direction::ToServer { - (&mut tx.ft_ts.file, tx.ft_ts.file_flags) - } else { - (&mut tx.ft_tc.file, tx.ft_tc.file_flags) - }; - let added = (c.HTPFileCloseHandleRange)( + let added = if let Some(sfcm) = unsafe { SURICATA_HTTP2_FILE_CONFIG } { + let (files, flags) = if dir == Direction::ToServer { + (&mut tx.ft_ts.file, tx.ft_ts.file_flags) + } else { + (&mut tx.ft_tc.file, tx.ft_tc.file_flags) + }; + unsafe { + let added = SCHTPFileCloseHandleRange( sfcm.files_sbcfg, files, flags, @@ -181,10 +212,8 @@ pub fn http2_range_close(tx: &mut HTTP2Transaction, dir: Direction, data: &[u8]) data.as_ptr(), data.len() as u32, ); - (c.HttpRangeFreeBlock)(tx.file_range); + SCHttpRangeFreeBlock(tx.file_range); added - } else { - false } } else { false diff --git a/src/app-layer-htp-file.c b/src/app-layer-htp-file.c index de16435ec9..3b7b10dc9c 100644 --- a/src/app-layer-htp-file.c +++ b/src/app-layer-htp-file.c @@ -264,7 +264,7 @@ end: * \retval true if reassembled file was added * \retval false if no reassembled file was added */ -bool HTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *files, +bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *files, const uint16_t flags, HttpRangeContainerBlock *c, const uint8_t *data, uint32_t data_len) { bool added = false; @@ -340,11 +340,11 @@ int HTPFileClose( if (tx->file_range != NULL) { bool added = - HTPFileCloseHandleRange(&htp_sbcfg, files, flags, tx->file_range, data, data_len); + SCHTPFileCloseHandleRange(&htp_sbcfg, files, flags, tx->file_range, data, data_len); if (added) { tx->tx_data.files_opened++; } - HttpRangeFreeBlock(tx->file_range); + SCHttpRangeFreeBlock(tx->file_range); tx->file_range = NULL; } diff --git a/src/app-layer-htp-file.h b/src/app-layer-htp-file.h index 1fac823ce4..4cfe5531e0 100644 --- a/src/app-layer-htp-file.h +++ b/src/app-layer-htp-file.h @@ -31,7 +31,7 @@ int HTPFileOpen( HtpState *, HtpTxUserData *, const uint8_t *, uint16_t, const uint8_t *, uint32_t, uint8_t); int HTPFileOpenWithRange(HtpState *, HtpTxUserData *, const uint8_t *, uint16_t, const uint8_t *, uint32_t, const htp_tx_t *, const bstr *rawvalue, HtpTxUserData *htud); -bool HTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t, +bool SCHTPFileCloseHandleRange(const StreamingBufferConfig *sbcfg, FileContainer *, const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t); int HTPFileStoreChunk(HtpTxUserData *, const uint8_t *, uint32_t, uint8_t); diff --git a/src/app-layer-htp-range.c b/src/app-layer-htp-range.c index 9c7e7e6380..de9a18cfc4 100644 --- a/src/app-layer-htp-range.c +++ b/src/app-layer-htp-range.c @@ -604,7 +604,7 @@ static void HttpRangeBlockDerefContainer(HttpRangeContainerBlock *b) } } -void HttpRangeFreeBlock(HttpRangeContainerBlock *b) +void SCHttpRangeFreeBlock(HttpRangeContainerBlock *b) { if (b) { DEBUG_VALIDATE_BUG_ON(b->container == NULL && b->files != NULL); diff --git a/src/app-layer-htp-range.h b/src/app-layer-htp-range.h index 8fb561020b..dd51c7268e 100644 --- a/src/app-layer-htp-range.h +++ b/src/app-layer-htp-range.h @@ -109,7 +109,7 @@ HttpRangeContainerBlock *HttpRangeContainerOpenFile(const unsigned char *key, ui const unsigned char *name, uint16_t name_len, uint16_t flags, const unsigned char *data, uint32_t data_len); -void HttpRangeFreeBlock(HttpRangeContainerBlock *b); +void SCHttpRangeFreeBlock(HttpRangeContainerBlock *b); uint64_t HTPByteRangeMemcapGlobalCounter(void); uint64_t HTPByteRangeMemuseGlobalCounter(void); diff --git a/src/app-layer-htp.c b/src/app-layer-htp.c index d7862e0d75..424b62c7f3 100644 --- a/src/app-layer-htp.c +++ b/src/app-layer-htp.c @@ -486,8 +486,8 @@ static void HtpTxUserDataFree(void *txud) SCMimeStateFree(htud->mime_state); SCAppLayerTxDataCleanup(&htud->tx_data); if (htud->file_range) { - HTPFileCloseHandleRange(&htp_sbcfg, &htud->files_tc, 0, htud->file_range, NULL, 0); - HttpRangeFreeBlock(htud->file_range); + SCHTPFileCloseHandleRange(&htp_sbcfg, &htud->files_tc, 0, htud->file_range, NULL, 0); + SCHttpRangeFreeBlock(htud->file_range); } FileContainerRecycle(&htud->files_ts, &htp_sbcfg); FileContainerRecycle(&htud->files_tc, &htp_sbcfg); diff --git a/src/rust-context.c b/src/rust-context.c index 84e0e6bcce..e287b18a17 100644 --- a/src/rust-context.c +++ b/src/rust-context.c @@ -25,9 +25,6 @@ #include "util-var.h" const SuricataContext suricata_context = { - HttpRangeFreeBlock, - HTPFileCloseHandleRange, - FileOpenFileWithId, FileCloseFileById, FileAppendDataById, diff --git a/src/rust-context.h b/src/rust-context.h index 2ac4394e49..9d0ad68fd2 100644 --- a/src/rust-context.h +++ b/src/rust-context.h @@ -27,16 +27,9 @@ #include "util-file.h" -// hack for include orders cf SCSha256 -typedef struct HttpRangeContainerBlock HttpRangeContainerBlock; - struct AppLayerParser; typedef struct SuricataContext_ { - void (*HttpRangeFreeBlock)(HttpRangeContainerBlock *); - bool (*HTPFileCloseHandleRange)(const StreamingBufferConfig *sbcfg, FileContainer *, - const uint16_t, HttpRangeContainerBlock *, const uint8_t *, uint32_t); - int (*FileOpenFileWithId)(FileContainer *, const StreamingBufferConfig *, uint32_t track_id, const uint8_t *name, uint16_t name_len, const uint8_t *data, uint32_t data_len, uint16_t flags);