|
| 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 | + assert!(init.is_none(), "init must not be None"); |
| 39 | + assert!(deinit.is_none(), "deinit must not be None"); |
| 40 | + assert!(write.is_none(), "write must not be None"); |
| 41 | + assert!(thread_init.is_none(), "thread_init must not be None"); |
| 42 | + assert!(thread_deinit.is_none(), "thread_deinit must not be None"); |
| 43 | + |
| 44 | + let name = CString::new(name)?; |
| 45 | + |
| 46 | + let inner = Box::new(SCEveFileType { |
| 47 | + name: name.as_ptr(), |
| 48 | + Init: init, |
| 49 | + ThreadInit: thread_init, |
| 50 | + Write: write, |
| 51 | + ThreadDeinit: thread_deinit, |
| 52 | + Deinit: deinit, |
| 53 | + entries: Default::default(), |
| 54 | + }); |
| 55 | + |
| 56 | + Ok(Self { inner, _name: name }) |
| 57 | + } |
| 58 | + |
| 59 | + fn as_mut_ptr(&mut self) -> *mut SCEveFileType { |
| 60 | + &mut *self.inner |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub fn register_file_type(mut ft: EveFileType) -> bool { |
| 65 | + if unsafe { SCRegisterEveFileType(ft.as_mut_ptr()) } { |
| 66 | + // Forget the file type now, as its now owned by the EVE |
| 67 | + // filetype registry. |
| 68 | + std::mem::forget(ft); |
| 69 | + true |
| 70 | + } else { |
| 71 | + false |
| 72 | + } |
| 73 | +} |
0 commit comments