rust/ffi: use ThreadVars wrapper in flow callbacks

Update the flow init, update and finish callback registrations to pass the
safe ThreadVars wrapper instead of a raw pointer.

Ticket: #8598
pull/15499/head
Jason Ish 4 months ago committed by Victor Julien
parent 1cac242948
commit 5e0abf1572

@ -104,18 +104,19 @@ The Rust wrappers register closures or function items and return
.. code-block:: rust .. code-block:: rust
use suricata_ffi::flow::{self, Flow, Packet, ThreadVars}; use suricata_ffi::flow::{self, Flow, Packet};
use suricata_ffi::thread::ThreadVars;
use suricata_ffi::SCLogNotice; use suricata_ffi::SCLogNotice;
fn flow_init(_tv: *mut ThreadVars, f: *mut Flow, _p: *const Packet) { fn flow_init(_tv: &mut ThreadVars, f: *mut Flow, _p: *const Packet) {
SCLogNotice!("flow initialized: {:p}", f); SCLogNotice!("flow initialized: {:p}", f);
} }
fn flow_update(_tv: *mut ThreadVars, f: *mut Flow, p: *mut Packet) { fn flow_update(_tv: &mut ThreadVars, f: *mut Flow, p: *mut Packet) {
SCLogNotice!("flow updated: {:p} packet: {:p}", f, p); SCLogNotice!("flow updated: {:p} packet: {:p}", f, p);
} }
fn flow_finish(_tv: *mut ThreadVars, f: *mut Flow) { fn flow_finish(_tv: &mut ThreadVars, f: *mut Flow) {
SCLogNotice!("flow finished: {:p}", f); SCLogNotice!("flow finished: {:p}", f);
} }

@ -73,11 +73,11 @@ fn on_thread_init(tv: &mut ThreadVars) {
); );
} }
fn log_flow_init(_tv: *mut sys::ThreadVars, _f: *mut Flow, _p: *const Packet) { fn log_flow_init(_tv: &mut ThreadVars, _f: *mut Flow, _p: *const Packet) {
SCLogNotice!("rust example flow init callback: flow={:p}", _f); SCLogNotice!("rust example flow init callback: flow={:p}", _f);
} }
fn log_flow_update(_tv: *mut sys::ThreadVars, _f: *mut Flow, _p: *mut Packet) { fn log_flow_update(_tv: &mut ThreadVars, _f: *mut Flow, _p: *mut Packet) {
SCLogNotice!( SCLogNotice!(
"rust example flow update callback: flow={:p}, packet={:p}", "rust example flow update callback: flow={:p}, packet={:p}",
_f, _f,
@ -85,7 +85,7 @@ fn log_flow_update(_tv: *mut sys::ThreadVars, _f: *mut Flow, _p: *mut Packet) {
); );
} }
fn log_flow_finish(_tv: *mut sys::ThreadVars, _f: *mut Flow) { fn log_flow_finish(_tv: &mut ThreadVars, _f: *mut Flow) {
SCLogNotice!("rust example flow finish callback: flow={:p}", _f); SCLogNotice!("rust example flow finish callback: flow={:p}", _f);
} }

@ -17,11 +17,13 @@
use std::os::raw::c_void; use std::os::raw::c_void;
use suricata_sys::sys::{Flow, Packet, ThreadVars};
use suricata_sys::sys::{ use suricata_sys::sys::{
SCFlowRegisterFinishCallback, SCFlowRegisterInitCallback, SCFlowRegisterUpdateCallback, self, Flow, Packet, SCFlowRegisterFinishCallback, SCFlowRegisterInitCallback,
SCFlowRegisterUpdateCallback,
}; };
use crate::thread::ThreadVars;
/// Register a flow initialization callback. /// Register a flow initialization callback.
/// ///
/// The callback is invoked whenever Suricata initializes a flow. It receives: /// The callback is invoked whenever Suricata initializes a flow. It receives:
@ -37,7 +39,7 @@ use suricata_sys::sys::{
/// The callback must not panic. /// The callback must not panic.
pub fn register_init_callback<F>(callback: F) -> Result<(), &'static str> pub fn register_init_callback<F>(callback: F) -> Result<(), &'static str>
where where
F: Fn(*mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
{ {
let user = Box::into_raw(Box::new(callback)) as *mut c_void; let user = Box::into_raw(Box::new(callback)) as *mut c_void;
if unsafe { SCFlowRegisterInitCallback(Some(init_callback_wrapper::<F>), user) } { if unsafe { SCFlowRegisterInitCallback(Some(init_callback_wrapper::<F>), user) } {
@ -66,7 +68,7 @@ where
/// The callback must not panic. /// The callback must not panic.
pub fn register_update_callback<F>(callback: F) -> Result<(), &'static str> pub fn register_update_callback<F>(callback: F) -> Result<(), &'static str>
where where
F: Fn(*mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
{ {
let user = Box::into_raw(Box::new(callback)) as *mut c_void; let user = Box::into_raw(Box::new(callback)) as *mut c_void;
if unsafe { SCFlowRegisterUpdateCallback(Some(update_callback_wrapper::<F>), user) } { if unsafe { SCFlowRegisterUpdateCallback(Some(update_callback_wrapper::<F>), user) } {
@ -93,7 +95,7 @@ where
/// The callback must not panic. /// The callback must not panic.
pub fn register_finish_callback<F>(callback: F) -> Result<(), &'static str> pub fn register_finish_callback<F>(callback: F) -> Result<(), &'static str>
where where
F: Fn(*mut ThreadVars, *mut Flow) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow) + Send + Sync + 'static,
{ {
let user = Box::into_raw(Box::new(callback)) as *mut c_void; let user = Box::into_raw(Box::new(callback)) as *mut c_void;
if unsafe { SCFlowRegisterFinishCallback(Some(finish_callback_wrapper::<F>), user) } { if unsafe { SCFlowRegisterFinishCallback(Some(finish_callback_wrapper::<F>), user) } {
@ -107,28 +109,31 @@ where
} }
unsafe extern "C" fn init_callback_wrapper<F>( unsafe extern "C" fn init_callback_wrapper<F>(
tv: *mut ThreadVars, f: *mut Flow, p: *const Packet, user: *mut c_void, tv: *mut sys::ThreadVars, f: *mut Flow, p: *const Packet, user: *mut c_void,
) where ) where
F: Fn(*mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
{ {
let callback = &*(user as *const F); let callback = &*(user as *const F);
callback(tv, f, p); let mut tv = ThreadVars::from_ptr(tv);
callback(&mut tv, f, p);
} }
unsafe extern "C" fn update_callback_wrapper<F>( unsafe extern "C" fn update_callback_wrapper<F>(
tv: *mut ThreadVars, f: *mut Flow, p: *mut Packet, user: *mut c_void, tv: *mut sys::ThreadVars, f: *mut Flow, p: *mut Packet, user: *mut c_void,
) where ) where
F: Fn(*mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
{ {
let callback = &*(user as *const F); let callback = &*(user as *const F);
callback(tv, f, p); let mut tv = ThreadVars::from_ptr(tv);
callback(&mut tv, f, p);
} }
unsafe extern "C" fn finish_callback_wrapper<F>( unsafe extern "C" fn finish_callback_wrapper<F>(
tv: *mut ThreadVars, f: *mut Flow, user: *mut c_void, tv: *mut sys::ThreadVars, f: *mut Flow, user: *mut c_void,
) where ) where
F: Fn(*mut ThreadVars, *mut Flow) + Send + Sync + 'static, F: Fn(&mut ThreadVars, *mut Flow) + Send + Sync + 'static,
{ {
let callback = &*(user as *const F); let callback = &*(user as *const F);
callback(tv, f); let mut tv = ThreadVars::from_ptr(tv);
callback(&mut tv, f);
} }

Loading…
Cancel
Save