aboutsummaryrefslogtreecommitdiff
path: root/rustables/src/expr/mod.rs
diff options
context:
space:
mode:
authorSimon THOBY <git@nightmared.fr>2021-10-20 20:59:56 +0200
committerSimon THOBY <git@nightmared.fr>2021-11-02 22:17:43 +0100
commite58ccb7d8b8168b801a820005c1cf8e50f892659 (patch)
tree2d92c578e2919ee940f0f9926d4482c42bcc443b /rustables/src/expr/mod.rs
parent3f61ea42bd291c208d07006d8019c25d588f9183 (diff)
Adds an iterator over expressions to Rules
Diffstat (limited to 'rustables/src/expr/mod.rs')
-rw-r--r--rustables/src/expr/mod.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/rustables/src/expr/mod.rs b/rustables/src/expr/mod.rs
index 99ea44b..a86b44d 100644
--- a/rustables/src/expr/mod.rs
+++ b/rustables/src/expr/mod.rs
@@ -3,9 +3,54 @@
//!
//! [`Rule`]: struct.Rule.html
+use std::ffi::CStr;
+use std::ffi::CString;
+use std::fmt::Debug;
+use std::rc::Rc;
+
use super::rule::Rule;
use rustables_sys::{self as sys, libc};
+pub struct ExpressionWrapper {
+ pub(crate) expr: *const sys::nftnl_expr,
+ // we also need the rule here to ensure that the rule lives as long as the `expr` pointer
+ #[allow(dead_code)]
+ pub(crate) rule: Rc<Rule>,
+}
+
+impl Debug for ExpressionWrapper {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{:?}", self.get_str())
+ }
+}
+
+impl ExpressionWrapper {
+ pub fn get_str(&self) -> CString {
+ let mut descr_buf = vec![0i8; 4096];
+ unsafe {
+ sys::nftnl_expr_snprintf(
+ descr_buf.as_mut_ptr(),
+ (descr_buf.len() - 1) as u64,
+ self.expr,
+ sys::NFTNL_OUTPUT_DEFAULT,
+ 0,
+ );
+ CStr::from_ptr(descr_buf.as_ptr()).to_owned()
+ }
+ }
+
+ pub fn get_expr_kind(&self) -> Option<&CStr> {
+ unsafe {
+ let ptr = sys::nftnl_expr_get_str(self.expr, sys::NFTNL_EXPR_NAME as u16);
+ if !ptr.is_null() {
+ Some(CStr::from_ptr(ptr))
+ } else {
+ None
+ }
+ }
+ }
+}
+
/// Trait for every safe wrapper of an nftables expression.
pub trait Expression {
/// Allocates and returns the low level `nftnl_expr` representation of this expression.