aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/chain_methods.rs78
-rw-r--r--src/lib.rs3
2 files changed, 81 insertions, 0 deletions
diff --git a/src/chain_methods.rs b/src/chain_methods.rs
new file mode 100644
index 0000000..81b5fd1
--- /dev/null
+++ b/src/chain_methods.rs
@@ -0,0 +1,78 @@
+use crate::{Batch, Chain, Hook, MsgType, Policy, Table};
+use std::ffi::{CString, NulError};
+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;
+ 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())?;
+ let mut chain = Chain::new(&chain_name, table);
+ chain.set_hook(direction.get_hook(), 0);
+ Ok(chain)
+ }
+ fn verdict(mut self, verdict: &Verdict) -> Self {
+ self.set_policy(verdict.get());
+ self
+ }
+ fn add_to_batch(self, batch: &mut Batch) -> Self {
+ batch.add(&self, MsgType::Add);
+ self
+ }
+}
+
+/// 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,
+ }
+ }
+}
+
diff --git a/src/lib.rs b/src/lib.rs
index 33bf12c..511b0ba 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -109,6 +109,9 @@ mod chain;
pub use chain::{get_chains_cb, list_chains_for_table};
pub use chain::{Chain, ChainType, Hook, Policy, Priority};
+mod chain_methods;
+pub use chain_methods::{ChainMethods, Direction, Verdict};
+
pub mod query;
mod rule;