From 7d024a840c9d6b3d61ec4ebfdefbca771b332cc5 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Tue, 2 Jun 2026 22:44:00 +0200 Subject: [PATCH] rust: bindgen detect ThreadCtx keyword functions --- rust/sys/src/sys.rs | 27 +++++++++++++++++++++------ src/detect-engine-buffer.h | 7 +++++++ src/detect-engine.c | 7 ++++--- src/detect-engine.h | 4 ---- src/detect-http-cookie.c | 6 +++--- src/detect-http-header-common.c | 4 ++-- src/detect-http-header-names.c | 8 ++++---- src/detect-http-header.c | 20 ++++++++++---------- src/detect-http-headers-stub.h | 6 +++--- src/detect-http-host.c | 9 +++++---- src/detect-http-raw-header.c | 4 ++-- src/detect-http-start.c | 4 ++-- src/detect-http-ua.c | 4 ++-- 13 files changed, 65 insertions(+), 45 deletions(-) diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index f281ee90ae..c1ef09a326 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -648,6 +648,12 @@ pub struct DetectEngineCtx_ { pub type DetectEngineCtx = DetectEngineCtx_; #[repr(C)] #[derive(Debug, Copy, Clone)] +pub struct DetectEngineThreadCtx_ { + _unused: [u8; 0], +} +pub type DetectEngineThreadCtx = DetectEngineThreadCtx_; +#[repr(C)] +#[derive(Debug, Copy, Clone)] pub struct Signature_ { _unused: [u8; 0], } @@ -668,6 +674,21 @@ extern "C" { s: *mut Signature, transform: ::std::os::raw::c_int, options: *mut ::std::os::raw::c_void, ) -> ::std::os::raw::c_int; } +extern "C" { + pub fn SCDetectRegisterThreadCtxGlobalFuncs( + name: *const ::std::os::raw::c_char, + InitFunc: ::std::option::Option< + unsafe extern "C" fn(arg1: *mut ::std::os::raw::c_void) -> *mut ::std::os::raw::c_void, + >, + data: *mut ::std::os::raw::c_void, + FreeFunc: ::std::option::Option, + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn SCDetectThreadCtxGetGlobalKeywordThreadCtx( + det_ctx: *mut DetectEngineThreadCtx, id: ::std::os::raw::c_int, + ) -> *mut ::std::os::raw::c_void; +} #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct InspectionBuffer { @@ -699,12 +720,6 @@ impl Default for InspectionBuffer { } #[repr(C)] #[derive(Debug, Copy, Clone)] -pub struct DetectEngineThreadCtx_ { - _unused: [u8; 0], -} -pub type DetectEngineThreadCtx = DetectEngineThreadCtx_; -#[repr(C)] -#[derive(Debug, Copy, Clone)] pub struct DetectEngineTransforms { _unused: [u8; 0], } diff --git a/src/detect-engine-buffer.h b/src/detect-engine-buffer.h index 0a6663d85b..d98b1298d0 100644 --- a/src/detect-engine-buffer.h +++ b/src/detect-engine-buffer.h @@ -27,6 +27,7 @@ // types from detect.h with only forward declarations for bindgen // could be #ifndef SURICATA_BINDGEN_H #include "detect.h" #endif typedef struct DetectEngineCtx_ DetectEngineCtx; +typedef struct DetectEngineThreadCtx_ DetectEngineThreadCtx; typedef struct Signature_ Signature; typedef struct SigMatch_ SigMatch; @@ -36,4 +37,10 @@ SigMatch *DetectBufferGetFirstSigMatch(const Signature *s, const uint32_t buf_id SigMatch *DetectBufferGetLastSigMatch(const Signature *s, const uint32_t buf_id); int SCDetectSignatureAddTransform(Signature *s, int transform, void *options); +// Declared here for bindgen +// But defined in detect-engine.c to access g_master_de_ctx +int SCDetectRegisterThreadCtxGlobalFuncs( + const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *)); +void *SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id); + #endif /* SURICATA_DETECT_ENGINE_BUFFER_H */ diff --git a/src/detect-engine.c b/src/detect-engine.c index 6bdbdbae27..f44b57f00f 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -40,6 +40,7 @@ #include "detect-engine-sigorder.h" #include "detect-engine-build.h" +#include "detect-engine-buffer.h" #include "detect-engine-siggroup.h" #include "detect-engine-address.h" #include "detect-engine-port.h" @@ -3895,8 +3896,8 @@ void *DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id) * \retval id for retrieval of ctx at runtime * \retval -1 on error */ -int DetectRegisterThreadCtxGlobalFuncs(const char *name, - void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *)) +int SCDetectRegisterThreadCtxGlobalFuncs( + const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *)) { int id; BUG_ON(InitFunc == NULL || FreeFunc == NULL); @@ -3939,7 +3940,7 @@ int DetectRegisterThreadCtxGlobalFuncs(const char *name, * * \retval ctx or NULL on error */ -void *DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id) +void *SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id) { if (id < 0 || id > det_ctx->global_keyword_ctxs_size || det_ctx->global_keyword_ctxs_array == NULL) { diff --git a/src/detect-engine.h b/src/detect-engine.h index 2318c1da6e..9e11dba0ba 100644 --- a/src/detect-engine.h +++ b/src/detect-engine.h @@ -80,10 +80,6 @@ DetectEngineCtx *DetectEngineCtxInitStubForMT(void); void DetectEngineCtxFree(DetectEngineCtx *); int DetectEngineThreadCtxGetJsonContext(DetectEngineThreadCtx *det_ctx); -int DetectRegisterThreadCtxGlobalFuncs(const char *name, - void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *)); -void *DetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id); - TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **); TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *); bool DetectEngineMpmCachingEnabled(void); diff --git a/src/detect-http-cookie.c b/src/detect-http-cookie.c index bc15c974de..f687232a7a 100644 --- a/src/detect-http-cookie.c +++ b/src/detect-http-cookie.c @@ -132,7 +132,7 @@ void DetectHttpCookieRegister(void) DetectBufferTypeSetDescriptionByName("http_cookie", "http cookie header"); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http_cookie", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_cookie_buffer_id = DetectBufferTypeGetByName("http_cookie"); @@ -238,7 +238,7 @@ static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetCookie(txv, STREAM_TOSERVER, &b, &b_len, thread_buf) != 1) @@ -261,7 +261,7 @@ static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetCookie(txv, STREAM_TOCLIENT, &b, &b_len, thread_buf) != 1) diff --git a/src/detect-http-header-common.c b/src/detect-http-header-common.c index 3ec0294e17..7c03b98c04 100644 --- a/src/detect-http-header-common.c +++ b/src/detect-http-header-common.c @@ -28,6 +28,7 @@ #include "detect.h" #include "detect-parse.h" #include "detect-engine.h" +#include "detect-engine-buffer.h" #include "detect-engine-mpm.h" #include "detect-engine-state.h" #include "detect-engine-prefilter.h" @@ -101,8 +102,7 @@ HttpHeaderBuffer *HttpHeaderGetBufferSpace(DetectEngineThreadCtx *det_ctx, uint8 { *ret_hdr_td = NULL; - HttpHeaderThreadData *hdr_td = - DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, keyword_id); + HttpHeaderThreadData *hdr_td = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, keyword_id); if (hdr_td == NULL) return NULL; *ret_hdr_td = hdr_td; diff --git a/src/detect-http-header-names.c b/src/detect-http-header-names.c index 9a475b3690..4b6285a4cf 100644 --- a/src/detect-http-header-names.c +++ b/src/detect-http-header-names.c @@ -170,7 +170,7 @@ static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; @@ -249,10 +249,10 @@ void DetectHttpHeaderNamesRegister(void) g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); - g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME, - HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); + g_keyword_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( + KEYWORD_NAME, HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http2.header_names", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); SCLogDebug("keyword %s registered. Thread id %d. " diff --git a/src/detect-http-header.c b/src/detect-http-header.c index 112f901c8c..a303796a5b 100644 --- a/src/detect-http-header.c +++ b/src/detect-http-header.c @@ -152,7 +152,7 @@ static InspectionBuffer *GetBuffer2ForTX(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetHeaders(txv, flow_flags, &b, &b_len, thread_buf) != 1) @@ -413,9 +413,9 @@ void DetectHttpHeaderRegister(void) g_http_header_buffer_id = DetectBufferTypeGetByName("http_header"); - g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_header", - HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_keyword_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( + "http_header", HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http2.header", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); } @@ -472,7 +472,7 @@ static bool GetHttp2HeaderData(DetectEngineThreadCtx *det_ctx, const void *txv, } else { kw_thread_id = g_h2_response_header_thread_id; } - void *hdr_td = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id); + void *hdr_td = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id); if (unlikely(hdr_td == NULL)) { return false; } @@ -491,7 +491,7 @@ static bool GetHttp1HeaderData(DetectEngineThreadCtx *det_ctx, const void *txv, kw_thread_id = g_response_header_thread_id; } HttpMultiBufHeaderThreadData *hdr_td = - DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id); + SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, kw_thread_id); if (unlikely(hdr_td == NULL)) { return false; } @@ -582,9 +582,9 @@ void DetectHttpRequestHeaderRegister(void) DetectBufferTypeSetDescriptionByName("http_request_header", "HTTP header name and value"); g_http_request_header_buffer_id = DetectBufferTypeGetByName("http_request_header"); DetectBufferTypeSupportsMultiInstance("http_request_header"); - g_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_request_header", + g_request_header_thread_id = SCDetectRegisterThreadCtxGlobalFuncs("http_request_header", HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree); - g_h2_request_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http2_request_header", + g_h2_request_header_thread_id = SCDetectRegisterThreadCtxGlobalFuncs("http2_request_header", SCHttp2ThreadMultiBufDataInit, NULL, SCHttp2ThreadMultiBufDataFree); } @@ -618,9 +618,9 @@ void DetectHttpResponseHeaderRegister(void) DetectBufferTypeSetDescriptionByName("http_response_header", "HTTP header name and value"); g_http_response_header_buffer_id = DetectBufferTypeGetByName("http_response_header"); DetectBufferTypeSupportsMultiInstance("http_response_header"); - g_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http_response_header", + g_response_header_thread_id = SCDetectRegisterThreadCtxGlobalFuncs("http_response_header", HttpMultiBufHeaderThreadDataInit, NULL, HttpMultiBufHeaderThreadDataFree); - g_h2_response_header_thread_id = DetectRegisterThreadCtxGlobalFuncs("http2_response_header", + g_h2_response_header_thread_id = SCDetectRegisterThreadCtxGlobalFuncs("http2_response_header", SCHttp2ThreadMultiBufDataInit, NULL, SCHttp2ThreadMultiBufDataFree); } diff --git a/src/detect-http-headers-stub.h b/src/detect-http-headers-stub.h index 77104d304e..7b45ccda46 100644 --- a/src/detect-http-headers-stub.h +++ b/src/detect-http-headers-stub.h @@ -86,7 +86,7 @@ static InspectionBuffer *GetRequestData2(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetHeaderValue(txv, STREAM_TOSERVER, HEADER_NAME, &b, &b_len, thread_buf) != 1) @@ -143,7 +143,7 @@ static InspectionBuffer *GetResponseData2(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetHeaderValue(txv, STREAM_TOCLIENT, HEADER_NAME, &b, &b_len, thread_buf) != 1) @@ -221,7 +221,7 @@ static void DetectHttpHeadersRegisterStub(void) DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( 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 fbf109cc97..f32780c24c 100644 --- a/src/detect-http-host.c +++ b/src/detect-http-host.c @@ -127,7 +127,7 @@ void DetectHttpHHRegister(void) DetectBufferTypeSetDescriptionByName("http_host", "http host"); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http_host", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_host_buffer_id = DetectBufferTypeGetByName("http_host"); @@ -163,7 +163,7 @@ void DetectHttpHHRegister(void) DetectBufferTypeSetDescriptionByName("http_raw_host", "http raw host header"); - g_http2_raw_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_raw_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http_raw_host", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_raw_host_buffer_id = DetectBufferTypeGetByName("http_raw_host"); @@ -275,7 +275,7 @@ static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx, if (buffer->inspect == NULL) { uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetHostNorm(txv, &b, &b_len, thread_buf) != 1) @@ -297,7 +297,8 @@ static InspectionBuffer *GetRawData2(DetectEngineThreadCtx *det_ctx, if (buffer->inspect == NULL) { uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_raw_thread_id); + void *thread_buf = + SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_raw_thread_id); if (thread_buf == NULL) return NULL; diff --git a/src/detect-http-raw-header.c b/src/detect-http-raw-header.c index ebb95328ee..993fa951ff 100644 --- a/src/detect-http-raw-header.c +++ b/src/detect-http-raw-header.c @@ -128,7 +128,7 @@ void DetectHttpRawHeaderRegister(void) DetectBufferTypeRegisterValidateCallback("http_raw_header", DetectHttpRawHeaderValidateCallback); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http2.raw_header", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_raw_header_buffer_id = DetectBufferTypeGetByName("http_raw_header"); @@ -220,7 +220,7 @@ static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx, uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetHeadersRaw(txv, flow_flags, &b, &b_len, thread_buf) != 1) diff --git a/src/detect-http-start.c b/src/detect-http-start.c index 037f82880f..72a3b56d57 100644 --- a/src/detect-http-start.c +++ b/src/detect-http-start.c @@ -203,8 +203,8 @@ void DetectHttpStartRegister(void) g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME); - g_keyword_thread_id = DetectRegisterThreadCtxGlobalFuncs(KEYWORD_NAME, - HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); + g_keyword_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( + KEYWORD_NAME, HttpHeaderThreadDataInit, &g_td_config, HttpHeaderThreadDataFree); SCLogDebug("keyword %s registered. Thread id %d. " "Buffer %s registered. Buffer id %d", diff --git a/src/detect-http-ua.c b/src/detect-http-ua.c index c01942876c..aab0fe8121 100644 --- a/src/detect-http-ua.c +++ b/src/detect-http-ua.c @@ -116,7 +116,7 @@ void DetectHttpUARegister(void) DetectBufferTypeSetDescriptionByName("http_user_agent", "http user agent"); - g_http2_thread_id = DetectRegisterThreadCtxGlobalFuncs( + g_http2_thread_id = SCDetectRegisterThreadCtxGlobalFuncs( "http_user_agent", SCDetectThreadBufDataInit, NULL, SCDetectThreadBufDataFree); g_http_ua_buffer_id = DetectBufferTypeGetByName("http_user_agent"); @@ -196,7 +196,7 @@ static InspectionBuffer *GetData2(DetectEngineThreadCtx *det_ctx, if (buffer->inspect == NULL) { uint32_t b_len = 0; const uint8_t *b = NULL; - void *thread_buf = DetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); + void *thread_buf = SCDetectThreadCtxGetGlobalKeywordThreadCtx(det_ctx, g_http2_thread_id); if (thread_buf == NULL) return NULL; if (SCHttp2TxGetUserAgent(txv, &b, &b_len, thread_buf) != 1)