]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: a Rust ConfNode wrapper.
authorJason Ish <ish@unx.ca>
Wed, 13 Jun 2018 22:03:33 +0000 (16:03 -0600)
committerJason Ish <ish@unx.ca>
Fri, 15 Jun 2018 21:48:52 +0000 (15:48 -0600)
A Rust wrapper around the C ConfNode object. Currenlty only exposes
ConfGetChildValueBool and ConfGetChildValue.

rust/src/conf.rs

index 080396a6424c06e8aef76086fae50884d8cf23e3..58e88241459e9adebb106a2df5390f7da8a97cbf 100644 (file)
@@ -16,6 +16,8 @@
  */
 
 use std::os::raw::c_char;
+use std::os::raw::c_void;
+use std::os::raw::c_int;
 use std::ffi::{CString, CStr};
 use std::ptr;
 use std::str;
@@ -24,6 +26,10 @@ use log::*;
 
 extern {
     fn ConfGet(key: *const c_char, res: *mut *const c_char) -> i8;
+    fn ConfGetChildValue(conf: *const c_void, key: *const c_char,
+                         vptr: *mut *const c_char) -> i8;
+    fn ConfGetChildValueBool(conf: *const c_void, key: *const c_char,
+                             vptr: *mut c_int) -> i8;
 }
 
 // Return the string value of a configuration value.
@@ -65,3 +71,58 @@ pub fn conf_get_bool(key: &str) -> bool {
 
     return false;
 }
+
+/// Wrap a Suricata ConfNode and expose some of its methods with a
+/// Rust friendly interface.
+pub struct ConfNode {
+    pub conf: *const c_void,
+}
+
+impl ConfNode {
+
+    pub fn wrap(conf: *const c_void) -> ConfNode {
+        return ConfNode{
+            conf: conf,
+        }
+    }
+
+    pub fn get_child_value(&self, key: &str) -> Option<&str> {
+        let mut vptr: *const c_char = ptr::null_mut();
+
+        unsafe {
+            if ConfGetChildValue(self.conf,
+                                 CString::new(key).unwrap().as_ptr(),
+                                 &mut vptr) != 1 {
+                return None;
+            }
+        }
+
+        if vptr == ptr::null() {
+            return None;
+        }
+
+        let value = str::from_utf8(unsafe{
+            CStr::from_ptr(vptr).to_bytes()
+        }).unwrap();
+
+        return Some(value);
+    }
+
+    pub fn get_child_bool(&self, key: &str) -> bool {
+        let mut vptr: c_int = 0;
+
+        unsafe {
+            if ConfGetChildValueBool(self.conf,
+                                     CString::new(key).unwrap().as_ptr(),
+                                     &mut vptr) != 1 {
+                return false;
+            }
+        }
+
+        if vptr == 1 {
+            return true;
+        }
+        return false;
+    }
+
+}