From: Philippe Antoine Date: Thu, 20 Mar 2025 09:42:56 +0000 (+0100) Subject: conf: extend API for rust X-Git-Tag: suricata-8.0.0-beta1~64 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30af626a92ca0f90cb882ce4958921e951087613;p=thirdparty%2Fsuricata.git conf: extend API for rust Will allow rust to list the values of an array such as dns.types for outputs --- diff --git a/rust/src/conf.rs b/rust/src/conf.rs index 109c9b22de..944302b639 100644 --- a/rust/src/conf.rs +++ b/rust/src/conf.rs @@ -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 { 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 { + 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 { + let node = unsafe { SCConfGetFirstNode(self.conf) }; + if node.is_null() { + None + } else { + Some(ConfNode::wrap(node)) + } + } + + pub fn next(&self) -> Option { + 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(); diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index 87ea366bba..3979985345 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -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; +} diff --git a/src/conf.c b/src/conf.c index 93706843fc..20fb859fe4 100644 --- a/src/conf.c +++ b/src/conf.c @@ -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. */ diff --git a/src/conf.h b/src/conf.h index f857d1bfa9..d05a7394a7 100644 --- a/src/conf.h +++ b/src/conf.h @@ -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 */