aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon THOBY <git@nightmared.fr>2022-12-09 23:37:32 +0100
committerSimon THOBY <git@nightmared.fr>2022-12-09 23:37:32 +0100
commit603d540a45c968ad48071a73e1452f87abea377b (patch)
tree1ef2823e6f0c7805c61632f971b5fe1279827e3d
parent5fbf51008eb7e2d55e074cd12c877b59f4a41f79 (diff)
re-impl the masquerade expression
-rw-r--r--src/expr/masquerade.rs28
-rw-r--r--src/expr/mod.rs11
-rw-r--r--src/expr/wrapper.rs61
-rw-r--r--tests/expr.rs63
4 files changed, 47 insertions, 116 deletions
diff --git a/src/expr/masquerade.rs b/src/expr/masquerade.rs
index c1a06de..dce787f 100644
--- a/src/expr/masquerade.rs
+++ b/src/expr/masquerade.rs
@@ -1,24 +1,20 @@
-use super::{DeserializationError, Expression, Rule};
-use crate::sys;
-use std::os::raw::c_char;
+use rustables_macros::nfnetlink_struct;
+
+use super::Expression;
/// Sets the source IP to that of the output interface.
-#[derive(Debug, PartialEq)]
+#[derive(Default, Debug, PartialEq, Eq)]
+#[nfnetlink_struct(nested = true)]
pub struct Masquerade;
-impl Expression for Masquerade {
- fn get_raw_name() -> *const sys::libc::c_char {
- b"masq\0" as *const _ as *const c_char
- }
-
- fn from_expr(_expr: *const sys::nftnl_expr) -> Result<Self, DeserializationError>
- where
- Self: Sized,
- {
- Ok(Masquerade)
+impl Clone for Masquerade {
+ fn clone(&self) -> Self {
+ Masquerade {}
}
+}
- fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr {
- try_alloc!(unsafe { sys::nftnl_expr_alloc(Self::get_raw_name()) })
+impl Expression for Masquerade {
+ fn get_name() -> &'static str {
+ "masq"
}
}
diff --git a/src/expr/mod.rs b/src/expr/mod.rs
index a6da2cd..cfc01c8 100644
--- a/src/expr/mod.rs
+++ b/src/expr/mod.rs
@@ -40,10 +40,10 @@ pub use self::log::*;
mod lookup;
pub use self::lookup::*;
+*/
mod masquerade;
pub use self::masquerade::*;
-*/
mod meta;
pub use self::meta::*;
@@ -63,12 +63,6 @@ pub use self::register::Register;
mod verdict;
pub use self::verdict::*;
-/*
-
-mod wrapper;
-pub use self::wrapper::ExpressionWrapper;
-*/
-
#[derive(Debug, Error)]
pub enum ExpressionError {
#[error("The log prefix string is more than 127 characters long")]
@@ -223,7 +217,8 @@ create_expr_variant!(
[Nat, Nat],
[Payload, Payload],
[Cmp, Cmp],
- [Conntrack, Conntrack]
+ [Conntrack, Conntrack],
+ [Masquerade, Masquerade]
);
#[derive(Debug, Clone, PartialEq, Eq, Default)]
diff --git a/src/expr/wrapper.rs b/src/expr/wrapper.rs
deleted file mode 100644
index 12ef60b..0000000
--- a/src/expr/wrapper.rs
+++ /dev/null
@@ -1,61 +0,0 @@
-use std::ffi::CStr;
-use std::ffi::CString;
-use std::fmt::Debug;
-use std::rc::Rc;
-use std::os::raw::c_char;
-
-use super::{DeserializationError, Expression};
-use crate::{sys, Rule};
-
-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 {
- /// Retrieves a textual description of the expression.
- pub fn get_str(&self) -> CString {
- let mut descr_buf = vec![0i8; 4096];
- unsafe {
- sys::nftnl_expr_snprintf(
- descr_buf.as_mut_ptr() as *mut c_char,
- (descr_buf.len() - 1) as u64,
- self.expr,
- sys::NFTNL_OUTPUT_DEFAULT,
- 0,
- );
- CStr::from_ptr(descr_buf.as_ptr() as *mut c_char).to_owned()
- }
- }
-
- /// Retrieves the type of expression ("log", "counter", ...).
- pub fn get_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
- }
- }
- }
-
- /// Attempts to decode the expression as the type T.
- pub fn decode_expr<T: Expression>(&self) -> Result<T, DeserializationError> {
- if let Some(kind) = self.get_kind() {
- let raw_name = unsafe { CStr::from_ptr(T::get_raw_name()) };
- if kind == raw_name {
- return T::from_expr(self.expr);
- }
- }
- Err(DeserializationError::InvalidExpressionKind)
- }
-}
diff --git a/tests/expr.rs b/tests/expr.rs
index 4367116..da98677 100644
--- a/tests/expr.rs
+++ b/tests/expr.rs
@@ -1,8 +1,8 @@
use rustables::{
expr::{
Bitwise, Cmp, CmpOp, Conntrack, ConntrackKey, Counter, ExpressionList, HeaderField,
- HighLevelPayload, IcmpCode, Immediate, Log, Meta, MetaType, Nat, NatType, Register, Reject,
- RejectType, TCPHeaderField, TransportHeaderField, VerdictKind,
+ HighLevelPayload, IcmpCode, Immediate, Log, Masquerade, Meta, MetaType, Nat, NatType,
+ Register, Reject, RejectType, TCPHeaderField, TransportHeaderField, VerdictKind,
},
sys::{
NFTA_BITWISE_DREG, NFTA_BITWISE_LEN, NFTA_BITWISE_MASK, NFTA_BITWISE_SREG,
@@ -335,35 +335,36 @@ fn log_expr_is_valid() {
// .to_raw()
// );
//}
-//
-//use rustables::expr::Masquerade;
-//#[test]
-//fn masquerade_expr_is_valid() {
-// let masquerade = Masquerade;
-// let mut rule = get_test_rule();
-// let (nlmsghdr, _nfgenmsg, raw_expr) = get_test_nlmsg_from_expr(&mut rule, &masquerade);
-// assert_eq!(nlmsghdr.nlmsg_len, 76);
-//
-// assert_eq!(
-// raw_expr,
-// NetlinkExpr::List(vec![
-// NetlinkExpr::Final(NFTA_RULE_TABLE, TABLE_NAME.to_vec()),
-// NetlinkExpr::Final(NFTA_RULE_CHAIN, CHAIN_NAME.to_vec()),
-// NetlinkExpr::Nested(
-// NFTA_RULE_EXPRESSIONS,
-// vec![NetlinkExpr::Nested(
-// NFTA_LIST_ELEM,
-// vec![
-// NetlinkExpr::Final(NFTA_EXPR_NAME, b"masq\0".to_vec()),
-// NetlinkExpr::Nested(NFTA_EXPR_DATA, vec![]),
-// ]
-// )]
-// )
-// ])
-// .to_raw()
-// );
-//}
-//
+
+#[test]
+fn masquerade_expr_is_valid() {
+ let masquerade = Masquerade::default();
+ let mut rule = get_test_rule().with_expressions(vec![masquerade]);
+
+ let mut buf = Vec::new();
+ let (nlmsghdr, _nfgenmsg, raw_expr) = get_test_nlmsg(&mut buf, &mut rule);
+ assert_eq!(nlmsghdr.nlmsg_len, 72);
+
+ assert_eq!(
+ raw_expr,
+ NetlinkExpr::List(vec![
+ NetlinkExpr::Final(NFTA_RULE_TABLE, TABLE_NAME.as_bytes().to_vec()),
+ NetlinkExpr::Final(NFTA_RULE_CHAIN, CHAIN_NAME.as_bytes().to_vec()),
+ NetlinkExpr::Nested(
+ NFTA_RULE_EXPRESSIONS,
+ vec![NetlinkExpr::Nested(
+ NFTA_LIST_ELEM,
+ vec![
+ NetlinkExpr::Final(NFTA_EXPR_NAME, b"masq".to_vec()),
+ NetlinkExpr::Nested(NFTA_EXPR_DATA, vec![]),
+ ]
+ )]
+ )
+ ])
+ .to_raw()
+ );
+}
+
#[test]
fn meta_expr_is_valid() {
let meta = Meta::default()