PfTable

Struct PfTable 

Source
pub struct PfTable {
    pub name: String,
}
Expand description

A high-level struct representing a pf table containing addresses

Fields§

§name: String

Implementations§

Source§

impl PfTable

Source

pub fn new(name: &str) -> PfTable

Prepares a new PfTable with a provided name

Examples found in repository?
examples/my_table.rs (line 18)
6fn main() -> Result<(), Box<dyn Error>> {
7    let fd = fs::OpenOptions::new()
8        .write(true)
9        .open("/dev/pf")?;
10
11    let mut addrs = vec![
12        PfrAddr::new_host(IpAddr::V4("127.0.0.1".parse()?)),
13        PfrAddr::new_host(IpAddr::V4("127.0.0.2".parse()?)),
14        PfrAddr::new_host(IpAddr::V4("127.0.0.3".parse()?)),
15        PfrAddr::new_host(IpAddr::V6("::1".parse()?)),
16    ];
17
18    let mut table = PfTable::new("my_table");
19
20    // Add a list of addresses to table
21    table.add_addrs(&fd, addrs.clone())?;
22
23    // Delete a list of addresses from table
24    let last = addrs.pop().unwrap();
25    table.del_addrs(&fd, vec![last])?;
26
27    // Print contents of table
28    for addr in table.get_addrs(&fd)? {
29        println!("{}", addr);
30    }
31
32    // Clear all addresses from table
33    table.clr_addrs(&fd)?;
34
35    Ok(())
36}
Source

pub fn get_addrs(&self, fd: &File) -> Result<Vec<PfrAddr>, PfError>

Asks the kernel for a list of addresses in the table

Examples found in repository?
examples/my_table.rs (line 28)
6fn main() -> Result<(), Box<dyn Error>> {
7    let fd = fs::OpenOptions::new()
8        .write(true)
9        .open("/dev/pf")?;
10
11    let mut addrs = vec![
12        PfrAddr::new_host(IpAddr::V4("127.0.0.1".parse()?)),
13        PfrAddr::new_host(IpAddr::V4("127.0.0.2".parse()?)),
14        PfrAddr::new_host(IpAddr::V4("127.0.0.3".parse()?)),
15        PfrAddr::new_host(IpAddr::V6("::1".parse()?)),
16    ];
17
18    let mut table = PfTable::new("my_table");
19
20    // Add a list of addresses to table
21    table.add_addrs(&fd, addrs.clone())?;
22
23    // Delete a list of addresses from table
24    let last = addrs.pop().unwrap();
25    table.del_addrs(&fd, vec![last])?;
26
27    // Print contents of table
28    for addr in table.get_addrs(&fd)? {
29        println!("{}", addr);
30    }
31
32    // Clear all addresses from table
33    table.clr_addrs(&fd)?;
34
35    Ok(())
36}
Source

pub fn add_addrs(&self, fd: &File, addrs: Vec<PfrAddr>) -> Result<(), PfError>

Asks the kernel to add a list of addresses to the table

Examples found in repository?
examples/my_table.rs (line 21)
6fn main() -> Result<(), Box<dyn Error>> {
7    let fd = fs::OpenOptions::new()
8        .write(true)
9        .open("/dev/pf")?;
10
11    let mut addrs = vec![
12        PfrAddr::new_host(IpAddr::V4("127.0.0.1".parse()?)),
13        PfrAddr::new_host(IpAddr::V4("127.0.0.2".parse()?)),
14        PfrAddr::new_host(IpAddr::V4("127.0.0.3".parse()?)),
15        PfrAddr::new_host(IpAddr::V6("::1".parse()?)),
16    ];
17
18    let mut table = PfTable::new("my_table");
19
20    // Add a list of addresses to table
21    table.add_addrs(&fd, addrs.clone())?;
22
23    // Delete a list of addresses from table
24    let last = addrs.pop().unwrap();
25    table.del_addrs(&fd, vec![last])?;
26
27    // Print contents of table
28    for addr in table.get_addrs(&fd)? {
29        println!("{}", addr);
30    }
31
32    // Clear all addresses from table
33    table.clr_addrs(&fd)?;
34
35    Ok(())
36}
Source

pub fn del_addrs(&self, fd: &File, addrs: Vec<PfrAddr>) -> Result<(), PfError>

Asks the kernel to delete a list of addresses from the table

Examples found in repository?
examples/my_table.rs (line 25)
6fn main() -> Result<(), Box<dyn Error>> {
7    let fd = fs::OpenOptions::new()
8        .write(true)
9        .open("/dev/pf")?;
10
11    let mut addrs = vec![
12        PfrAddr::new_host(IpAddr::V4("127.0.0.1".parse()?)),
13        PfrAddr::new_host(IpAddr::V4("127.0.0.2".parse()?)),
14        PfrAddr::new_host(IpAddr::V4("127.0.0.3".parse()?)),
15        PfrAddr::new_host(IpAddr::V6("::1".parse()?)),
16    ];
17
18    let mut table = PfTable::new("my_table");
19
20    // Add a list of addresses to table
21    table.add_addrs(&fd, addrs.clone())?;
22
23    // Delete a list of addresses from table
24    let last = addrs.pop().unwrap();
25    table.del_addrs(&fd, vec![last])?;
26
27    // Print contents of table
28    for addr in table.get_addrs(&fd)? {
29        println!("{}", addr);
30    }
31
32    // Clear all addresses from table
33    table.clr_addrs(&fd)?;
34
35    Ok(())
36}
Source

pub fn clr_addrs(&self, fd: &File) -> Result<(), PfError>

Asks the kernel to remove every address from the table

Examples found in repository?
examples/my_table.rs (line 33)
6fn main() -> Result<(), Box<dyn Error>> {
7    let fd = fs::OpenOptions::new()
8        .write(true)
9        .open("/dev/pf")?;
10
11    let mut addrs = vec![
12        PfrAddr::new_host(IpAddr::V4("127.0.0.1".parse()?)),
13        PfrAddr::new_host(IpAddr::V4("127.0.0.2".parse()?)),
14        PfrAddr::new_host(IpAddr::V4("127.0.0.3".parse()?)),
15        PfrAddr::new_host(IpAddr::V6("::1".parse()?)),
16    ];
17
18    let mut table = PfTable::new("my_table");
19
20    // Add a list of addresses to table
21    table.add_addrs(&fd, addrs.clone())?;
22
23    // Delete a list of addresses from table
24    let last = addrs.pop().unwrap();
25    table.del_addrs(&fd, vec![last])?;
26
27    // Print contents of table
28    for addr in table.get_addrs(&fd)? {
29        println!("{}", addr);
30    }
31
32    // Clear all addresses from table
33    table.clr_addrs(&fd)?;
34
35    Ok(())
36}

Trait Implementations§

Source§

impl Clone for PfTable

Source§

fn clone(&self) -> PfTable

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PfTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.