Skip to content

Commit 211844e

Browse files
committed
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: OISF#8598
1 parent de2096c commit 211844e

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

doc/userguide/devguide/extending/flow-lifecycle-callbacks.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,19 @@ The Rust wrappers register closures or function items and return
104104

105105
.. code-block:: rust
106106
107-
use suricata_ffi::flow::{self, Flow, Packet, ThreadVars};
107+
use suricata_ffi::flow::{self, Flow, Packet};
108+
use suricata_ffi::thread::ThreadVars;
108109
use suricata_ffi::SCLogNotice;
109110
110-
fn flow_init(_tv: *mut ThreadVars, f: *mut Flow, _p: *const Packet) {
111+
fn flow_init(_tv: &ThreadVars, f: *mut Flow, _p: *const Packet) {
111112
SCLogNotice!("flow initialized: {:p}", f);
112113
}
113114
114-
fn flow_update(_tv: *mut ThreadVars, f: *mut Flow, p: *mut Packet) {
115+
fn flow_update(_tv: &ThreadVars, f: *mut Flow, p: *mut Packet) {
115116
SCLogNotice!("flow updated: {:p} packet: {:p}", f, p);
116117
}
117118
118-
fn flow_finish(_tv: *mut ThreadVars, f: *mut Flow) {
119+
fn flow_finish(_tv: &ThreadVars, f: *mut Flow) {
119120
SCLogNotice!("flow finished: {:p}", f);
120121
}
121122

examples/plugins/rust/src/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ fn on_thread_init(tv: &ThreadVars) {
7373
);
7474
}
7575

76-
fn log_flow_init(_tv: *mut sys::ThreadVars, _f: *mut Flow, _p: *const Packet) {
76+
fn log_flow_init(_tv: &ThreadVars, _f: *mut Flow, _p: *const Packet) {
7777
SCLogNotice!("rust example flow init callback: flow={:p}", _f);
7878
}
7979

80-
fn log_flow_update(_tv: *mut sys::ThreadVars, _f: *mut Flow, _p: *mut Packet) {
80+
fn log_flow_update(_tv: &ThreadVars, _f: *mut Flow, _p: *mut Packet) {
8181
SCLogNotice!(
8282
"rust example flow update callback: flow={:p}, packet={:p}",
8383
_f,
8484
_p
8585
);
8686
}
8787

88-
fn log_flow_finish(_tv: *mut sys::ThreadVars, _f: *mut Flow) {
88+
fn log_flow_finish(_tv: &ThreadVars, _f: *mut Flow) {
8989
SCLogNotice!("rust example flow finish callback: flow={:p}", _f);
9090
}
9191

rust/ffi/src/flow.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
use std::os::raw::c_void;
1919

20-
use suricata_sys::sys::{Flow, Packet, ThreadVars};
2120
use suricata_sys::sys::{
22-
SCFlowRegisterFinishCallback, SCFlowRegisterInitCallback, SCFlowRegisterUpdateCallback,
21+
self, Flow, Packet, SCFlowRegisterFinishCallback, SCFlowRegisterInitCallback,
22+
SCFlowRegisterUpdateCallback,
2323
};
2424

25+
use crate::thread::ThreadVars;
26+
2527
/// Register a flow initialization callback.
2628
///
2729
/// The callback is invoked whenever Suricata initializes a flow. It receives:
@@ -37,7 +39,7 @@ use suricata_sys::sys::{
3739
/// The callback must not panic.
3840
pub fn register_init_callback<F>(callback: F) -> Result<(), &'static str>
3941
where
40-
F: Fn(*mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
42+
F: Fn(&ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
4143
{
4244
let user = Box::into_raw(Box::new(callback)) as *mut c_void;
4345
if unsafe { SCFlowRegisterInitCallback(Some(init_callback_wrapper::<F>), user) } {
@@ -66,7 +68,7 @@ where
6668
/// The callback must not panic.
6769
pub fn register_update_callback<F>(callback: F) -> Result<(), &'static str>
6870
where
69-
F: Fn(*mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
71+
F: Fn(&ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
7072
{
7173
let user = Box::into_raw(Box::new(callback)) as *mut c_void;
7274
if unsafe { SCFlowRegisterUpdateCallback(Some(update_callback_wrapper::<F>), user) } {
@@ -93,7 +95,7 @@ where
9395
/// The callback must not panic.
9496
pub fn register_finish_callback<F>(callback: F) -> Result<(), &'static str>
9597
where
96-
F: Fn(*mut ThreadVars, *mut Flow) + Send + Sync + 'static,
98+
F: Fn(&ThreadVars, *mut Flow) + Send + Sync + 'static,
9799
{
98100
let user = Box::into_raw(Box::new(callback)) as *mut c_void;
99101
if unsafe { SCFlowRegisterFinishCallback(Some(finish_callback_wrapper::<F>), user) } {
@@ -107,28 +109,31 @@ where
107109
}
108110

109111
unsafe extern "C" fn init_callback_wrapper<F>(
110-
tv: *mut ThreadVars, f: *mut Flow, p: *const Packet, user: *mut c_void,
112+
tv: *mut sys::ThreadVars, f: *mut Flow, p: *const Packet, user: *mut c_void,
111113
) where
112-
F: Fn(*mut ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
114+
F: Fn(&ThreadVars, *mut Flow, *const Packet) + Send + Sync + 'static,
113115
{
114116
let callback = &*(user as *const F);
115-
callback(tv, f, p);
117+
let tv = ThreadVars::from_ptr(tv);
118+
callback(&tv, f, p);
116119
}
117120

118121
unsafe extern "C" fn update_callback_wrapper<F>(
119-
tv: *mut ThreadVars, f: *mut Flow, p: *mut Packet, user: *mut c_void,
122+
tv: *mut sys::ThreadVars, f: *mut Flow, p: *mut Packet, user: *mut c_void,
120123
) where
121-
F: Fn(*mut ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
124+
F: Fn(&ThreadVars, *mut Flow, *mut Packet) + Send + Sync + 'static,
122125
{
123126
let callback = &*(user as *const F);
124-
callback(tv, f, p);
127+
let tv = ThreadVars::from_ptr(tv);
128+
callback(&tv, f, p);
125129
}
126130

127131
unsafe extern "C" fn finish_callback_wrapper<F>(
128-
tv: *mut ThreadVars, f: *mut Flow, user: *mut c_void,
132+
tv: *mut sys::ThreadVars, f: *mut Flow, user: *mut c_void,
129133
) where
130-
F: Fn(*mut ThreadVars, *mut Flow) + Send + Sync + 'static,
134+
F: Fn(&ThreadVars, *mut Flow) + Send + Sync + 'static,
131135
{
132136
let callback = &*(user as *const F);
133-
callback(tv, f);
137+
let tv = ThreadVars::from_ptr(tv);
138+
callback(&tv, f);
134139
}

0 commit comments

Comments
 (0)