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
impl PfTable
Sourcepub fn new(name: &str) -> PfTable
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}
Sourcepub fn get_addrs(&self, fd: &File) -> Result<Vec<PfrAddr>, PfError>
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}
Sourcepub fn add_addrs(&self, fd: &File, addrs: Vec<PfrAddr>) -> Result<(), PfError>
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}
Sourcepub fn del_addrs(&self, fd: &File, addrs: Vec<PfrAddr>) -> Result<(), PfError>
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}
Sourcepub fn clr_addrs(&self, fd: &File) -> Result<(), PfError>
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§
Auto Trait Implementations§
impl Freeze for PfTable
impl RefUnwindSafe for PfTable
impl Send for PfTable
impl Sync for PfTable
impl Unpin for PfTable
impl UnwindSafe for PfTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more