rust: move HttpRangeFreeBlock out of SuricataContext

Ticket: 7667
pull/13884/head
Philippe Antoine 5 months ago committed by Victor Julien
parent 828a6bdbf8
commit 1f084f8cb7

@ -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,

@ -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;

@ -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

@ -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;
}

@ -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);

@ -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);

@ -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);

@ -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);

@ -25,9 +25,6 @@
#include "util-var.h"
const SuricataContext suricata_context = {
HttpRangeFreeBlock,
HTPFileCloseHandleRange,
FileOpenFileWithId,
FileCloseFileById,
FileAppendDataById,

@ -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);

Loading…
Cancel
Save