detect: make Http2ThreadBuf a generic DetectThreadBuf

to be used by snmp and other
pull/15835/head
Philippe Antoine 5 months ago committed by Victor Julien
parent b84f26ef0a
commit 5fe920ec42

@ -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<u8>,
}
#[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 <big|little|dce>

@ -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<u8>,
}
#[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

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

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

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

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

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

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

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

Loading…
Cancel
Save