|
| 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; |
| 19 | + |
| 20 | +use suricata_sys::sys::{ |
| 21 | + SCEveFileType, SCEveFileTypeDeinitFunc, SCEveFileTypeInitFunc, SCEveFileTypeThreadDeinitFunc, |
| 22 | + SCEveFileTypeThreadInitFunc, SCEveFileTypeWriteFunc, SCRegisterEveFileType, |
| 23 | +}; |
| 24 | + |
| 25 | +pub struct EveFileType { |
| 26 | + pub name: &'static str, |
| 27 | + pub init: SCEveFileTypeInitFunc, |
| 28 | + pub deinit: SCEveFileTypeDeinitFunc, |
| 29 | + pub write: SCEveFileTypeWriteFunc, |
| 30 | + pub thread_init: SCEveFileTypeThreadInitFunc, |
| 31 | + pub thread_deinit: SCEveFileTypeThreadDeinitFunc, |
| 32 | +} |
| 33 | + |
| 34 | +impl EveFileType { |
| 35 | + pub fn register(ft: Self) -> Result<(), &'static str> { |
| 36 | + let name = CString::new(ft.name).map_err(|_| "invalid name")?; |
| 37 | + if ft.init.is_none() { |
| 38 | + return Err("None not allowed for init"); |
| 39 | + } |
| 40 | + if ft.deinit.is_none() { |
| 41 | + return Err("None not allowed for deinit"); |
| 42 | + } |
| 43 | + if ft.write.is_none() { |
| 44 | + return Err("None now allowed for write"); |
| 45 | + } |
| 46 | + if ft.thread_init.is_none() { |
| 47 | + return Err("None now allowed for thread_init"); |
| 48 | + } |
| 49 | + if ft.thread_deinit.is_none() { |
| 50 | + return Err("None not allowed for thread_deinit"); |
| 51 | + } |
| 52 | + let mut cft = Box::new(SCEveFileType { |
| 53 | + name: name.as_ptr(), |
| 54 | + Init: ft.init, |
| 55 | + ThreadInit: ft.thread_init, |
| 56 | + Write: ft.write, |
| 57 | + ThreadDeinit: ft.thread_deinit, |
| 58 | + Deinit: ft.deinit, |
| 59 | + entries: Default::default(), |
| 60 | + }); |
| 61 | + if unsafe { SCRegisterEveFileType(&mut *cft) } { |
| 62 | + std::mem::forget(cft); |
| 63 | + std::mem::forget(name); |
| 64 | + Ok(()) |
| 65 | + } else { |
| 66 | + Err("Failed to register EveFileType") |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments