13 unstable releases (5 breaking)

0.6.1 Oct 2, 2024
0.5.0 Jul 24, 2024
0.4.5 Dec 28, 2022
0.4.4 Oct 8, 2021
0.1.0 Dec 20, 2017

#13 in macOS and iOS APIs

Download history 1912/week @ 2024-07-30 1901/week @ 2024-08-06 2040/week @ 2024-08-13 2214/week @ 2024-08-20 2773/week @ 2024-08-27 2564/week @ 2024-09-03 1342/week @ 2024-09-10 1491/week @ 2024-09-17 1760/week @ 2024-09-24 1956/week @ 2024-10-01 1435/week @ 2024-10-08 1750/week @ 2024-10-15 1756/week @ 2024-10-22 2008/week @ 2024-10-29 2047/week @ 2024-11-05 2569/week @ 2024-11-12

8,625 downloads per month

MIT/Apache

230KB
6.5K SLoC

pfctl

Library for interfacing with the Packet Filter (PF) firewall on macOS.

Allows controlling the PF firewall on macOS through ioctl syscalls and the /dev/pf device.

Reading and writing to /dev/pf requires root permissions. So any program using this crate must run as the superuser, otherwise creating the PfCtl instance will fail with a "Permission denied" error.

OS Compatibility

PF is the firewall used in most (all?) BSD systems, but this crate only supports the macOS variant for now. If it can be made to work on more BSD systems that would be great, but no work has been put into that so far.

Usage and examples

A lot of examples of how to use the various features of this crate can be found in the integration tests in and examples.

Here is a simple example showing how to enable the firewall and add a packet filtering rule:

extern crate pfctl;

// Create a PfCtl instance to control PF with:
let mut pf = pfctl::PfCtl::new().unwrap();

// Enable the firewall, equivalent to the command "pfctl -e":
pf.try_enable().unwrap();

// Add an anchor rule for packet filtering rules into PF. This will fail if it already exists,
// use `try_add_anchor` to avoid that:
let anchor_name = "testing-out-pfctl";
pf.add_anchor(anchor_name, pfctl::AnchorKind::Filter).unwrap();

// Create a packet filtering rule matching all packets on the "lo0" interface and allowing
// them to pass:
let rule = pfctl::FilterRuleBuilder::default()
    .action(pfctl::FilterRuleAction::Pass)
    .interface("lo0")
    .build()
    .unwrap();

// Add the filterig rule to the anchor we just created.
pf.add_rule(anchor_name, &rule).unwrap();

System bindings

See comments in generate_bindings.sh for how to generate the Rust bindings to the system libraries

License: MIT/Apache-2.0

Dependencies

~1–1.6MB
~34K SLoC