|
17 | 17 |
|
18 | 18 | //! Plugin utility module. |
19 | 19 |
|
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}; |
21 | 22 |
|
22 | 23 | pub fn init() { |
23 | 24 | unsafe { |
24 | 25 | crate::debug::set_log_level(SCLogGetLogLevel()); |
25 | 26 | } |
26 | 27 | } |
| 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