aboutsummaryrefslogtreecommitdiff
path: root/src/chain_methods.rs
diff options
context:
space:
mode:
authorlafleur <lafleur@boum.org>2021-11-09 20:24:31 +0100
committerlafleur <lafleur@boum.org>2021-11-09 20:24:31 +0100
commit5264331441e625d26bedd6ce9b5995713e4b5c57 (patch)
tree9074426c7f4c6683069aacc1b5117192a7e5a050 /src/chain_methods.rs
parent96e543c77c13cb1c2eb9b1220f1fc5c510deef6b (diff)
use native types in chain_methods
Diffstat (limited to 'src/chain_methods.rs')
-rw-r--r--src/chain_methods.rs78
1 files changed, 20 insertions, 58 deletions
diff --git a/src/chain_methods.rs b/src/chain_methods.rs
index 81b5fd1..8259d13 100644
--- a/src/chain_methods.rs
+++ b/src/chain_methods.rs
@@ -1,27 +1,35 @@
use crate::{Batch, Chain, Hook, MsgType, Policy, Table};
-use std::ffi::{CString, NulError};
+use std::ffi::CString;
use std::rc::Rc;
-use serde::{Deserialize, Serialize};
/// A helper trait over [`rustables::Chain`].
pub trait ChainMethods {
- /// Create a new Chain instance from a [`Direction`] over a [`rustables::Table`].
- fn from_direction(direction: &Direction, table: Rc<Table>) -> Result<Self, NulError> where Self: std::marker::Sized;
- /// Add a [`Verdict`] to the current Chain.
- fn verdict(self, verdict: &Verdict) -> Self;
+ /// Create a new Chain instance from a [`rustables::Hook`] over a [`rustables::Table`].
+ fn from_hook(hook: Hook, table: Rc<Table>) -> Self
+ where Self: std::marker::Sized;
+ /// Add a [`rustables::Policy`] to the current Chain.
+ fn verdict(self, policy: Policy) -> Self;
fn add_to_batch(self, batch: &mut Batch) -> Self;
}
+
impl ChainMethods for Chain {
- fn from_direction(direction: &Direction, table: Rc<Table>) -> Result<Self, NulError> {
- let chain_name = CString::new(direction.display())?;
+ fn from_hook(hook: Hook, table: Rc<Table>) -> Self {
+ let chain_name = match hook {
+ Hook::PreRouting => "prerouting",
+ Hook::Out => "out",
+ Hook::PostRouting => "postrouting",
+ Hook::Forward => "forward",
+ Hook::In => "in",
+ };
+ let chain_name = CString::new(chain_name).unwrap();
let mut chain = Chain::new(&chain_name, table);
- chain.set_hook(direction.get_hook(), 0);
- Ok(chain)
+ chain.set_hook(hook, 0);
+ chain
}
- fn verdict(mut self, verdict: &Verdict) -> Self {
- self.set_policy(verdict.get());
+ fn verdict(mut self, policy: Policy) -> Self {
+ self.set_policy(policy);
self
}
fn add_to_batch(self, batch: &mut Batch) -> Self {
@@ -30,49 +38,3 @@ impl ChainMethods for Chain {
}
}
-/// A Serializable wrapper type around [`rustables::Hook`].
-#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
-#[serde(rename_all = "snake_case")]
-pub enum Direction {
- Inbound,
- Outbound,
- Forward
-}
-impl Direction {
- /// Return the Direction's [`rustables::Hook`], ie its representation inside rustables. Note that
- /// there are Hooks not represented here, namely Prerouting and Postrouting. File a bug if
- /// you need those.
- pub fn get_hook(&self) -> Hook {
- match self {
- Direction::Inbound => Hook::In,
- Direction::Outbound => Hook::Out,
- Direction::Forward => Hook::Forward,
- }
- }
- /// Return a string representation of the Direction.
- pub fn display(&self) -> String {
- let s = match self {
- Direction::Inbound => "inbound",
- Direction::Outbound => "outbound",
- Direction::Forward => "forward",
- };
- s.to_string()
- }
-}
-/// A Serializable wrapper type around [`rustables::Policy`].
-#[derive(Serialize, Deserialize, Debug, Clone)]
-#[serde(rename_all = "snake_case")]
-pub enum Verdict {
- Accept,
- Drop
-}
-impl Verdict {
- /// Return the rustables representation of a Verdict (ie, a [`rustables::Policy`]).
- pub fn get(&self) -> Policy {
- match self {
- Verdict::Accept => Policy::Accept,
- Verdict::Drop => Policy::Drop,
- }
- }
-}
-