aboutsummaryrefslogtreecommitdiff
path: root/rustables/src/expr/lookup.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rustables/src/expr/lookup.rs')
-rw-r--r--rustables/src/expr/lookup.rs41
1 files changed, 32 insertions, 9 deletions
diff --git a/rustables/src/expr/lookup.rs b/rustables/src/expr/lookup.rs
index ac22440..7796b29 100644
--- a/rustables/src/expr/lookup.rs
+++ b/rustables/src/expr/lookup.rs
@@ -1,29 +1,52 @@
-use super::{Expression, Rule};
+use super::{DeserializationError, Expression, Rule};
use crate::set::Set;
use rustables_sys::{self as sys, libc};
-use std::ffi::CString;
+use std::ffi::{CStr, CString};
use std::os::raw::c_char;
+#[derive(Debug, PartialEq)]
pub struct Lookup {
set_name: CString,
set_id: u32,
}
impl Lookup {
- pub fn new<K>(set: &Set<'_, K>) -> Self {
- Lookup {
- set_name: set.get_name().to_owned(),
+ /// Creates a new lookup entry.
+ /// May return None if the set have no name.
+ pub fn new<K>(set: &Set<'_, K>) -> Option<Self> {
+ set.get_name().map(|set_name| Lookup {
+ set_name: set_name.to_owned(),
set_id: set.get_id(),
- }
+ })
}
}
impl Expression for Lookup {
+ fn get_raw_name() -> *const libc::c_char {
+ b"lookup\0" as *const _ as *const c_char
+ }
+
+ fn from_expr(expr: *const sys::nftnl_expr) -> Result<Self, DeserializationError>
+ where
+ Self: Sized,
+ {
+ unsafe {
+ let set_name = sys::nftnl_expr_get_str(expr, sys::NFTNL_EXPR_LOOKUP_SET as u16);
+ let set_id = sys::nftnl_expr_get_u32(expr, sys::NFTNL_EXPR_LOOKUP_SET_ID as u16);
+
+ if set_name.is_null() {
+ return Err(DeserializationError::NullPointer);
+ }
+
+ let set_name = CStr::from_ptr(set_name).to_owned();
+
+ Ok(Lookup { set_id, set_name })
+ }
+ }
+
fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr {
unsafe {
- let expr = try_alloc!(sys::nftnl_expr_alloc(
- b"lookup\0" as *const _ as *const c_char
- ));
+ let expr = try_alloc!(sys::nftnl_expr_alloc(Self::get_raw_name()));
sys::nftnl_expr_set_u32(
expr,