From 5fe920ec428c7528709c568f38c74863861fea65 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Tue, 2 Jun 2026 22:28:29 +0200 Subject: [PATCH] detect: make Http2ThreadBuf a generic DetectThreadBuf to be used by snmp and other --- rust/src/detect/mod.rs | 18 +++++++++++++++++- rust/src/http2/detect.rs | 34 +++++++++------------------------- src/detect-http-cookie.c | 2 +- src/detect-http-header-names.c | 2 +- src/detect-http-header.c | 2 +- src/detect-http-headers-stub.h | 2 +- src/detect-http-host.c | 4 ++-- src/detect-http-raw-header.c | 2 +- src/detect-http-ua.c | 2 +- 9 files changed, 34 insertions(+), 34 deletions(-) diff --git a/rust/src/detect/mod.rs b/rust/src/detect/mod.rs index e3f04a0ffa..ef90ca3482 100644 --- a/rust/src/detect/mod.rs +++ b/rust/src/detect/mod.rs @@ -36,7 +36,7 @@ pub mod uint; pub mod uri; pub mod vlan; -use std::ffi::CString; +use std::ffi::{c_void, CString}; /// EnumString trait that will be implemented on enums that /// derive StringEnum. @@ -91,6 +91,22 @@ pub use suricata_sys::sys::{ SIGMATCH_OPTIONAL_OPT, SIGMATCH_QUOTES_MANDATORY, SIGMATCH_SUPPORT_FIREWALL, }; +#[derive(Default)] +pub(crate) struct DetectThreadBuf { + pub data: Vec, +} + +#[no_mangle] +pub unsafe extern "C" fn SCDetectThreadBufDataInit(_cfg: *mut c_void) -> *mut c_void { + let boxed = Box::new(DetectThreadBuf::default()); + return Box::into_raw(boxed) as *mut c_void; +} + +#[no_mangle] +pub unsafe extern "C" fn SCDetectThreadBufDataFree(ctx: *mut c_void) { + std::mem::drop(Box::from_raw(ctx as *mut DetectThreadBuf)); +} + #[repr(u8)] #[derive(Copy, Clone, Debug, PartialEq, Eq)] // endian diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index a2af42ee1d..40df833bf0 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -23,7 +23,7 @@ use crate::detect::uint::{ detect_match_uint, detect_parse_array_uint_enum, detect_uint_match_at_index, DetectUintArrayData, DetectUintData, DetectUintIndex, DetectUintMode, }; -use crate::detect::EnumString; +use crate::detect::{DetectThreadBuf, EnumString}; use crate::direction::Direction; use base64::{engine::general_purpose::STANDARD, Engine}; use std::ffi::CStr; @@ -572,7 +572,7 @@ pub unsafe extern "C" fn SCHttp2TxGetMethod( pub unsafe extern "C" fn SCHttp2TxGetHost( tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, ":authority") { match value { Http2Header::Single(v) => { @@ -644,7 +644,7 @@ fn http2_normalize_host(ve: Http2Header) -> Http2Header { pub unsafe extern "C" fn SCHttp2TxGetHostNorm( tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, ":authority") { match http2_normalize_host(value) { Http2Header::Single(v) => { @@ -666,7 +666,7 @@ pub unsafe extern "C" fn SCHttp2TxGetHostNorm( pub unsafe extern "C" fn SCHttp2TxGetUserAgent( tx: &mut HTTP2Transaction, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "user-agent") { match value { Http2Header::Single(v) => { @@ -701,7 +701,7 @@ pub unsafe extern "C" fn SCHttp2TxGetCookie( tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); if direction == u8::from(Direction::ToServer) { if let Some(value) = http2_frames_get_header_value(tx, Direction::ToServer, "cookie") { match value { @@ -740,7 +740,7 @@ pub unsafe extern "C" fn SCHttp2TxGetHeaderValue( tx: &mut HTTP2Transaction, direction: u8, strname: *const std::os::raw::c_char, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); let hname: &CStr = CStr::from_ptr(strname); //unsafe if let Ok(s) = hname.to_str() { if let Some(value) = http2_frames_get_header_value(tx, direction.into(), &s.to_lowercase()) @@ -772,28 +772,12 @@ fn http2_escape_header(blocks: &[parser::HTTP2FrameHeaderBlock], i: u32) -> Vec< return vec; } -#[derive(Default)] -struct Http2ThreadBuf { - data: Vec, -} - -#[no_mangle] -pub unsafe extern "C" fn SCHttp2ThreadBufDataInit(_cfg: *mut c_void) -> *mut c_void { - let boxed = Box::new(Http2ThreadBuf::default()); - return Box::into_raw(boxed) as *mut c_void; -} - -#[no_mangle] -pub unsafe extern "C" fn SCHttp2ThreadBufDataFree(ctx: *mut c_void) { - std::mem::drop(Box::from_raw(ctx as *mut Http2ThreadBuf)); -} - #[no_mangle] pub unsafe extern "C" fn SCHttp2TxGetHeaderNames( tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); tbuf.data.clear(); tbuf.data.extend_from_slice(b"\r\n"); let frames = if direction & Direction::ToServer as u8 != 0 { @@ -857,7 +841,7 @@ pub unsafe extern "C" fn SCHttp2TxGetHeaders( tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); tbuf.data.clear(); let frames = if direction & Direction::ToServer as u8 != 0 { &tx.frames_ts @@ -891,7 +875,7 @@ pub unsafe extern "C" fn SCHttp2TxGetHeadersRaw( tx: &mut HTTP2Transaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32, tbuf: *mut c_void, ) -> u8 { - let tbuf = cast_pointer!(tbuf, Http2ThreadBuf); + let tbuf = cast_pointer!(tbuf, DetectThreadBuf); tbuf.data.clear(); let frames = if direction & Direction::ToServer as u8 != 0 { &tx.frames_ts diff --git a/src/detect-http-cookie.c b/src/detect-http-cookie.c index 0a1d818efe..bc15c974de 100644 --- a/src/detect-http-cookie.c +++ b/src/detect-http-cookie.c @@ -133,7 +133,7 @@ void DetectHttpCookieRegister(void) "http cookie header"); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http_cookie", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http_cookie", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_cookie_buffer_id = DetectBufferTypeGetByName("http_cookie"); } diff --git a/src/detect-http-header-names.c b/src/detect-http-header-names.c index 3d4e3286a0..9a475b3690 100644 --- a/src/detect-http-header-names.c +++ b/src/detect-http-header-names.c @@ -253,7 +253,7 @@ void DetectHttpHeaderNamesRegister(void) HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http2.header_names", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http2.header_names", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); SCLogDebug("keyword %s registered. Thread id %d. " "Buffer %s registered. Buffer id %d", diff --git a/src/detect-http-header.c b/src/detect-http-header.c index cdaa8c6e88..112f901c8c 100644 --- a/src/detect-http-header.c +++ b/src/detect-http-header.c @@ -416,7 +416,7 @@ void DetectHttpHeaderRegister(void) g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header", HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http2.header", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http2.header", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); } static int g_http_request_header_buffer_id = 0; diff --git a/src/detect-http-headers-stub.h b/src/detect-http-headers-stub.h index a0d20a62e4..77104d304e 100644 --- a/src/detect-http-headers-stub.h +++ b/src/detect-http-headers-stub.h @@ -222,7 +222,7 @@ static void DetectHttpHeadersRegisterStub(void) DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - BUFFER_NAME, SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + BUFFER_NAME, SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); } diff --git a/src/detect-http-host.c b/src/detect-http-host.c index bd233cd802..fbf109cc97 100644 --- a/src/detect-http-host.c +++ b/src/detect-http-host.c @@ -128,7 +128,7 @@ void DetectHttpHHRegister(void) "http host"); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http_host", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http_host", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_host_buffer_id = DetectBufferTypeGetByName("http_host"); @@ -164,7 +164,7 @@ void DetectHttpHHRegister(void) "http raw host header"); g_http2_raw_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http_raw_host", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http_raw_host", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host"); } diff --git a/src/detect-http-raw-header.c b/src/detect-http-raw-header.c index eb81228bbe..ebb95328ee 100644 --- a/src/detect-http-raw-header.c +++ b/src/detect-http-raw-header.c @@ -129,7 +129,7 @@ void DetectHttpRawHeaderRegister(void) DetectBufferTypeRegisterValidateCallback("http_raw_header", DetectHttpRawHeaderValidateCallback); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http2.raw_header", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http2.raw_header", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_raw_header_buffer_id = DetectBufferTypeGetByName("http_raw_header"); } diff --git a/src/detect-http-ua.c b/src/detect-http-ua.c index f507fb4bea..c01942876c 100644 --- a/src/detect-http-ua.c +++ b/src/detect-http-ua.c @@ -117,7 +117,7 @@ void DetectHttpUARegister(void) "http user agent"); g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( - "http_user_agent", SCHttp2ThreadBufDataInit, NULL, SCHttp2ThreadBufDataFree); + "http_user_agent", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent"); }