Skip to content

Commit f6a0aeb

Browse files
committed
rust/ffi: rust wrapper around eve filetype registration
1 parent f13ff20 commit f6a0aeb

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

rust/ffi/src/eve.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright (C) 2026 Open Information Security Foundation
2+
*
3+
* You can copy, redistribute or modify this Program under the terms of
4+
* the GNU General Public License version 2 as published by the Free
5+
* Software Foundation.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* version 2 along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15+
* 02110-1301, USA.
16+
*/
17+
18+
use std::ffi::{CString, NulError};
19+
20+
use suricata_sys::sys::{
21+
SCEveFileType, SCEveFileTypeDeinitFunc, SCEveFileTypeInitFunc, SCEveFileTypeThreadDeinitFunc,
22+
SCEveFileTypeThreadInitFunc, SCEveFileTypeWriteFunc, SCRegisterEveFileType,
23+
};
24+
25+
pub struct EveFileType {
26+
inner: Box<SCEveFileType>,
27+
28+
// Never read, just used to hold the real string.
29+
_name: CString,
30+
}
31+
32+
impl EveFileType {
33+
pub fn new(
34+
name: &str, init: SCEveFileTypeInitFunc, deinit: SCEveFileTypeDeinitFunc,
35+
write: SCEveFileTypeWriteFunc, thread_init: SCEveFileTypeThreadInitFunc,
36+
thread_deinit: SCEveFileTypeThreadDeinitFunc,
37+
) -> Result<Self, NulError> {
38+
// These are all required, but we can't enforce it with the
39+
// function signature. Instead assert for early detection
40+
// during development of an EveFileType.
41+
//
42+
// Perhaps look at the typestate builder pattern to enforce at
43+
// compile time.
44+
assert!(init.is_some(), "init must not be None");
45+
assert!(deinit.is_some(), "deinit must not be None");
46+
assert!(write.is_some(), "write must not be None");
47+
assert!(thread_init.is_some(), "thread_init must not be None");
48+
assert!(thread_deinit.is_some(), "thread_deinit must not be None");
49+
50+
let name = CString::new(name)?;
51+
52+
let inner = Box::new(SCEveFileType {
53+
name: name.as_ptr(),
54+
Init: init,
55+
ThreadInit: thread_init,
56+
Write: write,
57+
ThreadDeinit: thread_deinit,
58+
Deinit: deinit,
59+
entries: Default::default(),
60+
});
61+
62+
Ok(Self { inner, _name: name })
63+
}
64+
65+
fn as_mut_ptr(&mut self) -> *mut SCEveFileType {
66+
&mut *self.inner
67+
}
68+
}
69+
70+
pub fn register_file_type(mut ft: EveFileType) -> bool {
71+
if unsafe { SCRegisterEveFileType(ft.as_mut_ptr()) } {
72+
// Forget the file type now, as its now owned by the EVE
73+
// filetype registry.
74+
std::mem::forget(ft);
75+
true
76+
} else {
77+
false
78+
}
79+
}

rust/ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
*/
1717

1818
pub mod debug;
19+
pub mod eve;
1920
pub mod plugin;

0 commit comments

Comments
 (0)