]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
conf: extend API for rust
authorPhilippe Antoine <contact@catenacyber.fr>
Thu, 20 Mar 2025 09:42:56 +0000 (10:42 +0100)
committerPhilippe Antoine <pantoine@oisf.net>
Sat, 5 Apr 2025 19:40:18 +0000 (21:40 +0200)
Will allow rust to list the values of an array such as
dns.types for outputs

rust/src/conf.rs
rust/sys/src/sys.rs
src/conf.c
src/conf.h

index 109c9b22dea0c37ddd5b2c37a996a96ef04a9d56..944302b6396be04f43795cb834cbef8a183227f3 100644 (file)
@@ -34,6 +34,10 @@ use suricata_sys::sys::SCConfGetChildValue;
 use suricata_sys::sys::SCConfGetChildValueBool;
 use suricata_sys::sys::SCConfGetNode;
 use suricata_sys::sys::SCConfNode;
+use suricata_sys::sys::SCConfNodeLookupChild;
+use suricata_sys::sys::SCConfGetFirstNode;
+use suricata_sys::sys::SCConfGetNextNode;
+use suricata_sys::sys::SCConfGetValueNode;
 
 pub fn conf_get_node(key: &str) -> Option<ConfNode> {
     let key = if let Ok(key) = CString::new(key) {
@@ -92,6 +96,42 @@ impl ConfNode {
         return Self { conf };
     }
 
+    pub fn get_child_node(&self, key: &str) -> Option<ConfNode> {
+        let node = unsafe {
+            let s = CString::new(key).unwrap();
+            SCConfNodeLookupChild(self.conf, s.as_ptr())
+        };
+        if node.is_null() {
+            None
+        } else {
+            Some(ConfNode::wrap(node))
+        }
+    }
+
+    pub fn first(&self) -> Option<ConfNode> {
+        let node = unsafe { SCConfGetFirstNode(self.conf) };
+        if node.is_null() {
+            None
+        } else {
+            Some(ConfNode::wrap(node))
+        }
+    }
+
+    pub fn next(&self) -> Option<ConfNode> {
+        let node = unsafe { SCConfGetNextNode(self.conf) };
+        if node.is_null() {
+            None
+        } else {
+            Some(ConfNode::wrap(node))
+        }
+    }
+
+    pub fn value(&self) -> &str {
+        let vptr = unsafe { SCConfGetValueNode(self.conf) };
+        let value = std::str::from_utf8(unsafe { CStr::from_ptr(vptr).to_bytes() }).unwrap();
+        return value;
+    }
+
     pub fn get_child_value(&self, key: &str) -> Option<&str> {
         let mut vptr: *const c_char = ptr::null_mut();
 
index 87ea366bba421036ba6116136f12cf07cf5b11a3..397998534588ac7a1fead9719a99d53f2ab2b700 100644 (file)
@@ -328,3 +328,12 @@ extern "C" {
         parent: *mut SCConfNode, name: *const ::std::os::raw::c_char, final_: ::std::os::raw::c_int,
     ) -> *mut SCConfNode;
 }
+extern "C" {
+    pub fn SCConfGetFirstNode(parent: *const SCConfNode) -> *mut SCConfNode;
+}
+extern "C" {
+    pub fn SCConfGetNextNode(node: *const SCConfNode) -> *mut SCConfNode;
+}
+extern "C" {
+    pub fn SCConfGetValueNode(node: *const SCConfNode) -> *const ::std::os::raw::c_char;
+}
index 93706843fc23be9c3609da20a4beae543891adc4..20fb859fe426646ef5c70813a4f8b582f37b1623 100644 (file)
@@ -201,6 +201,21 @@ SCConfNode *SCConfGetNode(const char *name)
     return node;
 }
 
+SCConfNode *SCConfGetFirstNode(const SCConfNode *parent)
+{
+    return TAILQ_FIRST(&parent->head);
+}
+
+SCConfNode *SCConfGetNextNode(const SCConfNode *node)
+{
+    return TAILQ_NEXT(node, next);
+}
+
+const char *SCConfGetValueNode(const SCConfNode *node)
+{
+    return node->val;
+}
+
 /**
  * \brief Get the root configuration node.
  */
index f857d1bfa998e9195e60ad5a2756b3b5e9ed474d..d05a7394a76c81f0b6f0b7e8165b7b9e0efad2d2 100644 (file)
@@ -100,4 +100,9 @@ SCConfNode *SCConfSetIfaceNode(const char *ifaces_node_name, const char *iface);
 int SCConfSetRootAndDefaultNodes(const char *ifaces_node_name, const char *iface,
         SCConfNode **if_root, SCConfNode **if_default);
 SCConfNode *SCConfNodeGetNodeOrCreate(SCConfNode *parent, const char *name, int final);
+
+SCConfNode *SCConfGetFirstNode(const SCConfNode *parent);
+SCConfNode *SCConfGetNextNode(const SCConfNode *node);
+const char *SCConfGetValueNode(const SCConfNode *node);
+
 #endif /* ! SURICATA_CONF_H */