From 958344787f25bd6e04bd452e9f36f33b41821828 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Mon, 29 Sep 2025 16:36:08 +0200 Subject: [PATCH] rust: bindgen FileAppendData Ticket: 7667 --- rust/Makefile.am | 1 + rust/cbindgen.toml | 1 - rust/src/core.rs | 2 +- rust/src/mime/smtp.rs | 12 +----------- rust/sys/src/sys.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/bindgen.h | 1 + src/util-file.h | 44 +++++++++++++++++++++++-------------------- 7 files changed, 70 insertions(+), 33 deletions(-) diff --git a/rust/Makefile.am b/rust/Makefile.am index 7bb37c6050..fc6563f0bd 100644 --- a/rust/Makefile.am +++ b/rust/Makefile.am @@ -141,6 +141,7 @@ if HAVE_BINDGEN --default-enum-style rust \ --allowlist-type 'AppProto.*' \ --allowlist-function 'AppProto.*' \ + --allowlist-function 'FileAppendData' \ --allowlist-type 'SC.*' \ --allowlist-function 'SC.*' \ --allowlist-var 'SC.*' \ diff --git a/rust/cbindgen.toml b/rust/cbindgen.toml index 78c3fd7330..4798b2fc60 100644 --- a/rust/cbindgen.toml +++ b/rust/cbindgen.toml @@ -102,7 +102,6 @@ exclude = [ "DetectEngineThreadCtx", "GenericVar", "Flow", - "StreamingBufferConfig", "HttpRangeContainerBlock", "FileContainer", "JsonT", diff --git a/rust/src/core.rs b/rust/src/core.rs index 902b7d2329..204d9518bc 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -66,7 +66,7 @@ macro_rules!BIT_U64 { // // Function types for calls into C. // -pub enum StreamingBufferConfig {} +pub use suricata_sys::sys::StreamingBufferConfig; #[allow(non_snake_case)] #[repr(C)] diff --git a/rust/src/mime/smtp.rs b/rust/src/mime/smtp.rs index b4cc8dd32e..d0fe4165a7 100644 --- a/rust/src/mime/smtp.rs +++ b/rust/src/mime/smtp.rs @@ -17,14 +17,12 @@ use super::mime; use crate::core::StreamingBufferConfig; -use crate::filecontainer::FileContainer; use crate::utils::base64; use digest::Digest; use digest::Update; use md5::Md5; use std::ffi::CStr; -use std::os::raw::c_uchar; -use suricata_sys::sys::SCBasicSearchNocaseIndex; +use suricata_sys::sys::{FileAppendData, FileContainer, SCBasicSearchNocaseIndex}; #[repr(u8)] #[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Eq)] @@ -258,14 +256,6 @@ fn mime_smtp_process_headers(ctx: &mut MimeStateSMTP) -> (u32, bool) { return (warnings, encap); } -extern "C" { - // Defined in util-file.h - pub fn FileAppendData( - c: *mut FileContainer, sbcfg: *const StreamingBufferConfig, data: *const c_uchar, - data_len: u32, - ) -> std::os::raw::c_int; -} - fn hex(i: u8) -> Option { if i.is_ascii_digit() { return Some(i - b'0'); diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index 226f8f5981..b44d540026 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -753,6 +753,12 @@ pub struct AppLayerParserState_ { pub type AppLayerParserState = AppLayerParserState_; #[doc = " \\brief Data structure to store app layer decoder events."] pub type AppLayerDecoderEvents = AppLayerDecoderEvents_; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct File_ { + _unused: [u8; 0], +} +pub type File = File_; extern "C" { #[doc = " \\brief Given a protocol name, checks if the parser is enabled in\n the conf file.\n\n \\param alproto_name Name of the app layer protocol.\n\n \\retval 1 If enabled.\n \\retval 0 If disabled."] pub fn SCAppLayerParserConfParserEnabled( @@ -866,6 +872,42 @@ extern "C" { } #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct StreamingBufferConfig_ { + pub buf_size: u32, + #[doc = "< max concurrent memory regions. 0 means no limit."] + pub max_regions: u16, + #[doc = "< max gap size before a new region will be created."] + pub region_gap: u32, + pub Calloc: ::std::option::Option< + unsafe extern "C" fn(n: usize, size: usize) -> *mut ::std::os::raw::c_void, + >, + pub Realloc: ::std::option::Option< + unsafe extern "C" fn( + ptr: *mut ::std::os::raw::c_void, + orig_size: usize, + size: usize, + ) -> *mut ::std::os::raw::c_void, + >, + pub Free: + ::std::option::Option, +} +pub type StreamingBufferConfig = StreamingBufferConfig_; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct FileContainer_ { + pub head: *mut File, + pub tail: *mut File, +} +pub type FileContainer = FileContainer_; +extern "C" { + #[doc = " \\brief Store a chunk of file data in the flow. The open \"flowfile\"\n will be used.\n\n \\param ffc the container\n \\param data data chunk\n \\param data_len data chunk len\n\n \\retval 0 ok\n \\retval -1 error"] + pub fn FileAppendData( + arg1: *mut FileContainer, sbcfg: *const StreamingBufferConfig, data: *const u8, + data_len: u32, + ) -> ::std::os::raw::c_int; +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct GenericVar_ { #[doc = "< variable type, uses detection sm_type"] pub type_: u16, diff --git a/src/bindgen.h b/src/bindgen.h index fce704073b..c356208f0f 100644 --- a/src/bindgen.h +++ b/src/bindgen.h @@ -28,6 +28,7 @@ #include "stdint.h" #include "stdbool.h" +#include "stddef.h" #define WARN_UNUSED diff --git a/src/util-file.h b/src/util-file.h index 1376578299..8c88d6e303 100644 --- a/src/util-file.h +++ b/src/util-file.h @@ -29,10 +29,32 @@ // uses some structrues from rust uint16_t SCFileFlowFlagsToFlags(const uint16_t flow_file_flags, uint8_t direction); -#ifndef SURICATA_BINDGEN_H - #include "conf.h" #include "util-streaming-buffer.h" + +typedef struct File_ File; + +typedef struct FileContainer_ { + File *head; + File *tail; +} FileContainer; + +/** + * \brief Store a chunk of file data in the flow. The open "flowfile" + * will be used. + * + * \param ffc the container + * \param data data chunk + * \param data_len data chunk len + * + * \retval 0 ok + * \retval -1 error + */ +int FileAppendData(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data, + uint32_t data_len); + +#ifndef SURICATA_BINDGEN_H + #include "flow.h" /* Hack: Pulling rust.h to get the SCSha256 causes all sorts of problems with @@ -116,11 +138,6 @@ typedef struct File_ { uint32_t sid_max; } File; -typedef struct FileContainer_ { - File *head; - File *tail; -} FileContainer; - FileContainer *FileContainerAlloc(void); void FileContainerFree(FileContainer *, const StreamingBufferConfig *cfg); @@ -170,19 +187,6 @@ int FileCloseFileById(FileContainer *, const StreamingBufferConfig *sbcfg, uint3 int FileCloseFilePtr(File *ff, const StreamingBufferConfig *sbcfg, const uint8_t *data, uint32_t data_len, uint16_t flags); -/** - * \brief Store a chunk of file data in the flow. The open "flowfile" - * will be used. - * - * \param ffc the container - * \param data data chunk - * \param data_len data chunk len - * - * \retval 0 ok - * \retval -1 error - */ -int FileAppendData(FileContainer *, const StreamingBufferConfig *sbcfg, const uint8_t *data, - uint32_t data_len); int FileAppendDataById(FileContainer *, const StreamingBufferConfig *sbcfg, uint32_t track_id, const uint8_t *data, uint32_t data_len); int FileAppendGAPById(FileContainer *ffc, const StreamingBufferConfig *sbcfg, uint32_t track_id,