]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
decode/datalink: Add datalink value/name logic
authorJeff Lucovsky <jlucovsky@oisf.net>
Sat, 19 Oct 2024 12:02:20 +0000 (08:02 -0400)
committerVictor Julien <victor@inliniac.net>
Tue, 1 Apr 2025 08:16:58 +0000 (10:16 +0200)
Issue: 6954

Add Rust based logic that maintains a hash map of link type values and
their associated output names.

rust/src/utils/datalink.rs [new file with mode: 0644]
rust/src/utils/mod.rs

diff --git a/rust/src/utils/datalink.rs b/rust/src/utils/datalink.rs
new file mode 100644 (file)
index 0000000..b107b59
--- /dev/null
@@ -0,0 +1,77 @@
+/* Copyright (C) 2024 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+// Author: Jeff Lucovsky <jlucovsky@oisf.net>
+
+use std::collections::HashMap;
+use std::ffi::{CStr, CString};
+use std::os::raw::c_char;
+use std::ptr;
+
+fn get_datalink_value(map: &HashMap<i32, CString>, key: i32) -> Option<&CString> {
+    map.get(&key)
+}
+
+fn add_datalink_value(map: &mut HashMap<i32, CString>, key: i32, value: &CStr) {
+    if let Ok(str_value) = value.to_str() {
+        if let Ok(cstring_value) = CString::new(str_value) {
+            map.insert(key, cstring_value);
+        }
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn SCDatalinkInit() -> *mut HashMap<i32, CString> {
+    let map: HashMap<i32, CString> = HashMap::new();
+    Box::into_raw(Box::new(map))
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn SCDatalinkValueNameInsert(
+    map: *mut HashMap<i32, CString>, key: i32, value: *const c_char,
+) {
+    if map.is_null() || value.is_null() {
+        return;
+    }
+
+    let map = &mut *map;
+    let c_str = CStr::from_ptr(value);
+
+    add_datalink_value(map, key, c_str);
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn SCDatalinkValueToName(
+    map: *mut HashMap<i32, CString>, key: i32,
+) -> *const c_char {
+    if map.is_null() {
+        return std::ptr::null_mut();
+    }
+
+    let map = &*map;
+    match get_datalink_value(map, key) {
+        Some(value) => value.as_ptr(),
+        None => ptr::null(),
+    }
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn SCDatalinkDeInit(map: *mut HashMap<i32, CString>) {
+    if !map.is_null() {
+        let _ = Box::from_raw(map); // Automatically dropped at end of scope
+    }
+}
index 64e48092f71168a616586c822e84bf2c280c207b..882126ca50fa81e6d8abde37b1f685d4198a9b31 100644 (file)
@@ -16,3 +16,4 @@
  */
 
 pub mod base64;
+pub mod datalink;