Skip to content

Commit 6a2bc68

Browse files
committed
rust/ffi: more rust friendly plugin registration
A plugin can now create a "Plugin" struct with Rust strings. The `into_raw` method converts it to a raw pointer suitable for returning during plugin registration.
1 parent 5531150 commit 6a2bc68

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

rust/ffi/src/plugin.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,52 @@
1717

1818
//! Plugin utility module.
1919
20-
use suricata_sys::sys::SCLogGetLogLevel;
20+
use std::{ffi::CString, os::raw::c_char};
21+
use suricata_sys::sys::{SCLogGetLogLevel, SCPlugin, SC_API_VERSION, SC_PACKAGE_VERSION};
2122

2223
pub fn init() {
2324
unsafe {
2425
crate::debug::set_log_level(SCLogGetLogLevel());
2526
}
2627
}
28+
29+
pub struct Plugin {
30+
pub name: &'static str,
31+
32+
/// Plugin version.
33+
pub version: &'static str,
34+
pub license: &'static str,
35+
pub author: &'static str,
36+
pub init: unsafe extern "C" fn(),
37+
}
38+
39+
impl Plugin {
40+
/// Convert the plugin into a raw pointer suitable for plugin
41+
/// registration.
42+
pub fn into_raw(self) -> *mut SCPlugin {
43+
let name = CString::new(self.name)
44+
.expect("plugin name must not contain NUL bytes")
45+
.into_raw() as *const c_char;
46+
let plugin_version = CString::new(self.version)
47+
.expect("plugin version must not contain NUL bytes")
48+
.into_raw() as *const c_char;
49+
let license = CString::new(self.license)
50+
.expect("plugin license must not contain NUL bytes")
51+
.into_raw() as *const c_char;
52+
let author = CString::new(self.author)
53+
.expect("plugin author must not contain NUL bytes")
54+
.into_raw() as *const c_char;
55+
56+
let plugin = SCPlugin {
57+
version: SC_API_VERSION,
58+
suricata_version: SC_PACKAGE_VERSION.as_ptr() as *const c_char,
59+
name,
60+
plugin_version,
61+
license,
62+
author,
63+
Init: Some(self.init),
64+
};
65+
66+
Box::into_raw(Box::new(plugin))
67+
}
68+
}

0 commit comments

Comments
 (0)