pub struct RIPTables {
pub cmd: &'static str,
pub has_check: bool,
pub has_wait: bool,
}
Fields§
§cmd: &'static str
The utility command which must be ‘iptables’ or ‘ip6tables’.
has_check: bool
Indicates if iptables has -C (–check) option
has_wait: bool
Indicates if iptables has -w (–wait) option
Implementations§
Source§impl RIPTables
impl RIPTables
Sourcepub fn execute<T>(&self, caller: T) -> RIPTResult<(i32, String)>
pub fn execute<T>(&self, caller: T) -> RIPTResult<(i32, String)>
Execute iptables command
§Example
let iptables = riptables::new(false).unwrap();
iptables.execute(|iptables| iptables.args(&["-t", "nat", "-A", "TESTNAT", "-j", "ACCEPT"])).is_ok();
Sourcepub fn get_policy<S>(&self, table: S, chain: S) -> RIPTResult<Option<String>>
pub fn get_policy<S>(&self, table: S, chain: S) -> RIPTResult<Option<String>>
Get the default policy for a table/chain.
§Example
let iptables = riptables::new(false).unwrap();
iptables.get_policy("filter", "INPUT").is_ok();
Sourcepub fn set_policy<S>(&self, table: S, chain: S, policy: S) -> RIPTResult<bool>
pub fn set_policy<S>(&self, table: S, chain: S, policy: S) -> RIPTResult<bool>
Set the default policy for a table/chain.
§Example
let iptables = riptables::new(false).unwrap();
iptables.set_policy("mangle", "FORWARD", "DROP").unwrap();
Sourcepub fn insert<S>(
&self,
table: S,
chain: S,
rule: S,
position: i32,
) -> RIPTResult<bool>
pub fn insert<S>( &self, table: S, chain: S, rule: S, position: i32, ) -> RIPTResult<bool>
Inserts rule
in the position
to the table/chain.
Returns true
if the rule is inserted.
§Example
let iptables = riptables::new(false).unwrap();
iptables.insert("nat", "TESTNAT", "-j ACCEPT", 1).unwrap();
Sourcepub fn insert_unique<S>(
&self,
table: S,
chain: S,
rule: S,
position: i32,
) -> RIPTResult<bool>
pub fn insert_unique<S>( &self, table: S, chain: S, rule: S, position: i32, ) -> RIPTResult<bool>
Inserts rule
in the position
to the table/chain if it does not exist.
Returns true
if the rule is inserted.
§Example
let iptables = riptables::new(false).unwrap();
iptables.insert_unique("nat", "TESTNAT", "-j ACCEPT", 1).unwrap();
Sourcepub fn replace<S>(
&self,
table: S,
chain: S,
rule: S,
position: i32,
) -> RIPTResult<bool>
pub fn replace<S>( &self, table: S, chain: S, rule: S, position: i32, ) -> RIPTResult<bool>
Replaces rule
in the position
to the table/chain.
Returns true
if the rule is replaced.
§Example
let iptables = riptables::new(false).unwrap();
iptables.replace("nat", "TESTNAT", "-j ACCEPT", 1).unwrap();
Sourcepub fn append<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn append<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Appends rule
to the table/chain.
Returns true
if the rule is appended.
§Example
let iptables = riptables::new(false).unwrap();
iptables.append("nat", "TESTNAT", "-m comment --comment \"double-quoted comment\" -j ACCEPT").unwrap();
Sourcepub fn append_unique<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn append_unique<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Appends rule
to the table/chain if it does not exist.
Returns true
if the rule is appended.
§Example
let iptables = riptables::new(false).unwrap();
iptables.append_unique("nat", "TESTNAT", "-m comment --comment \"double-quoted comment\" -j ACCEPT").unwrap();
Sourcepub fn append_replace<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn append_replace<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Appends or replaces rule
to the table/chain if it does not exist.
Returns true
if the rule is appended or replaced.
§Example
let iptables = riptables::new(false).unwrap();
iptables.append_replace("nat", "TESTNAT", "-m comment --comment \"double-quoted comment\" -j ACCEPT").unwrap();
Sourcepub fn delete<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn delete<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Deletes rule
from the table/chain.
Returns true
if the rule is deleted.
§Example
let iptables = riptables::new(false).unwrap();
iptables.delete("nat", "TESTNAT", "-j ACCEPT").unwrap();
Sourcepub fn delete_all<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn delete_all<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Deletes all repetition of the rule
from the table/chain.
Returns true
if the rules are deleted.
let iptables = riptables::new(false).unwrap();
iptables.delete_all("nat", "TESTNAT", "-j ACCEPT").unwrap();
Sourcepub fn list<S>(&self, table: S) -> RIPTResult<Vec<RIPTRule>>
pub fn list<S>(&self, table: S) -> RIPTResult<Vec<RIPTRule>>
Lists rules in the table/chain.
§Example
use riptables::rule::{Archive, RIPTRule};
let iptables = riptables::new(false).unwrap();
let table = "nat";
let name = "TESTNAT";
iptables.new_chain(table, name).unwrap();
iptables.insert(table, name, "-j ACCEPT", 1).unwrap();
let rules: Vec<RIPTRule> = iptables.list("nat").unwrap();
iptables.delete(table, name, "-j ACCEPT").unwrap();
iptables.delete_chain(table, name).unwrap();
println!("{}", rules.len());
for rule in rules {
println!("{:?}", rule);
println!("{:?}", rule.table);
println!("{:?}", rule.chain);
println!("{:?}", rule.origin);
}
Sourcepub fn chain_names<S>(&self, table: S) -> RIPTResult<Vec<String>>
pub fn chain_names<S>(&self, table: S) -> RIPTResult<Vec<String>>
Lists the name of each chain in the table.
§Example
let iptables = riptables::new(false).unwrap();
let names = iptables.chain_names("nat");
Sourcepub fn list_chains<S>(&self, table: S, chain: S) -> RIPTResult<Vec<RIPTRule>>
pub fn list_chains<S>(&self, table: S, chain: S) -> RIPTResult<Vec<RIPTRule>>
Lists rules in the table/chain.
§Example
use riptables::rule::RIPTRule;
let iptables = riptables::new(false).unwrap();
let rules: Vec<RIPTRule> = iptables.list_chains("nat", "INPUT").unwrap();
Sourcepub fn new_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
pub fn new_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
Creates a new user-defined chain.
Returns true
if the chain is created.
§Example
let iptables = riptables::new(false).unwrap();
iptables.new_chain("nat", "TESTNAT");
Sourcepub fn delete_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
pub fn delete_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
Deletes a user-defined chain in the table.
Returns true
if the chain is deleted.
§Example
let iptables = riptables::new(false).unwrap();
iptables.delete_chain("nat", "TESTNAT");
Sourcepub fn rename_chain<S>(
&self,
table: S,
old_chain: S,
new_chain: S,
) -> RIPTResult<bool>
pub fn rename_chain<S>( &self, table: S, old_chain: S, new_chain: S, ) -> RIPTResult<bool>
Renames a chain in the table.
Returns true
if the chain is renamed.
§Example
let iptables = riptables::new(false).unwrap();
iptables.rename_chain("nat", "TESTNAT", "OTHERNAME");
Sourcepub fn flush_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
pub fn flush_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
Flushes (deletes all rules) a chain.
Returns true
if the chain is flushed.
§Example
let iptables = riptables::new(false).unwrap();
iptables.flush_chain("nat", "TESTNAT");
Sourcepub fn exists_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
pub fn exists_chain<S>(&self, table: S, chain: S) -> RIPTResult<bool>
Checks for the existence of the chain
in the table.
Returns true if the chain exists.
§Example
let iptables = riptables::new(false).unwrap();
iptables.exists_chain("nat", "TESTNAT");
Sourcepub fn flush_table<S>(&self, table: S) -> RIPTResult<bool>
pub fn flush_table<S>(&self, table: S) -> RIPTResult<bool>
Flushes all chains in a table.
Returns true
if the chains are flushed.
§Example
let iptables = riptables::new(false).unwrap();
iptables.flush_table("nat");
Sourcepub fn list_tables<S>(&self, table: S) -> RIPTResult<Vec<RIPTRule>>
pub fn list_tables<S>(&self, table: S) -> RIPTResult<Vec<RIPTRule>>
Lists rules in the table.
§Example
use riptables::rule::RIPTRule;
let iptables = riptables::new(false).unwrap();
let rule: Vec<RIPTRule> = iptables.list_tables("nat").unwrap();
Sourcepub fn exists<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
pub fn exists<S>(&self, table: S, chain: S, rule: S) -> RIPTResult<bool>
Checks for the existence of the rule
in the table/chain.
Returns true if the rule exists.
§Example
let iptables = riptables::new(false).unwrap();
iptables.exists("nat", "TESTNAT", "-j ACCEPT").unwrap();