Skip to content

Commit da881c0

Browse files
committed
Switch from ioctl_sys to nix's ioctl
Allows to be built on other platforms like FreeBSD. Needs to stick to nix 0.26 because nix >= 0.27 requires Rust >= 1.65.
1 parent a1d6064 commit da881c0

File tree

4 files changed

+99
-67
lines changed

4 files changed

+99
-67
lines changed

Cargo.lock

Lines changed: 77 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ travis-ci = { repository = "mullvad/pfctl-rs" }
1717

1818
[dependencies]
1919
error-chain = "0.12.4"
20-
ioctl-sys = "0.8.0"
20+
nix = { version = "0.26.4", features = ["ioctl"], default-features = false }
2121
libc = "0.2.29"
2222
derive_builder = "0.9"
2323
ipnetwork = "0.20.0"

src/ffi/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use ioctl_sys::ioctl;
9+
use nix::{ioctl_none, ioctl_readwrite};
1010

1111
#[allow(non_camel_case_types)]
1212
#[allow(non_upper_case_globals)]
@@ -32,34 +32,34 @@ pub mod tcp {
3232
// The documentation describing the order of calls and accepted parameters can be found at:
3333
// http://man.openbsd.org/pf.4
3434
// DIOCSTART
35-
ioctl!(none pf_start with b'D', 1);
35+
ioctl_none!(pf_start, b'D', 1);
3636
// DIOCSTOP
37-
ioctl!(none pf_stop with b'D', 2);
37+
ioctl_none!(pf_stop, b'D', 2);
3838
// DIOCADDRULE
39-
ioctl!(readwrite pf_add_rule with b'D', 4; pfvar::pfioc_rule);
39+
ioctl_readwrite!(pf_add_rule, b'D', 4, pfvar::pfioc_rule);
4040
// DIOCGETRULES
41-
ioctl!(readwrite pf_get_rules with b'D', 6; pfvar::pfioc_rule);
41+
ioctl_readwrite!(pf_get_rules, b'D', 6, pfvar::pfioc_rule);
4242
// DIOCGETRULE
43-
ioctl!(readwrite pf_get_rule with b'D', 7; pfvar::pfioc_rule);
43+
ioctl_readwrite!(pf_get_rule, b'D', 7, pfvar::pfioc_rule);
4444
// DIOCCLRSTATES
45-
ioctl!(readwrite pf_clear_states with b'D', 18; pfvar::pfioc_state_kill);
45+
ioctl_readwrite!(pf_clear_states, b'D', 18, pfvar::pfioc_state_kill);
4646
// DIOCGETSTATUS
47-
ioctl!(readwrite pf_get_status with b'D', 21; pfvar::pf_status);
47+
ioctl_readwrite!(pf_get_status, b'D', 21, pfvar::pf_status);
4848
// DIOCGETSTATES
49-
ioctl!(readwrite pf_get_states with b'D', 25; pfvar::pfioc_states);
49+
ioctl_readwrite!(pf_get_states, b'D', 25, pfvar::pfioc_states);
5050
// DIOCCHANGERULE
51-
ioctl!(readwrite pf_change_rule with b'D', 26; pfvar::pfioc_rule);
51+
ioctl_readwrite!(pf_change_rule, b'D', 26, pfvar::pfioc_rule);
5252
// DIOCINSERTRULE
53-
ioctl!(readwrite pf_insert_rule with b'D', 27; pfvar::pfioc_rule);
53+
ioctl_readwrite!(pf_insert_rule, b'D', 27, pfvar::pfioc_rule);
5454
// DIOCDELETERULE
55-
ioctl!(readwrite pf_delete_rule with b'D', 28; pfvar::pfioc_rule);
55+
ioctl_readwrite!(pf_delete_rule, b'D', 28, pfvar::pfioc_rule);
5656
// DIOCKILLSTATES
57-
ioctl!(readwrite pf_kill_states with b'D', 41; pfvar::pfioc_state_kill);
57+
ioctl_readwrite!(pf_kill_states, b'D', 41, pfvar::pfioc_state_kill);
5858
// DIOCBEGINADDRS
59-
ioctl!(readwrite pf_begin_addrs with b'D', 51; pfvar::pfioc_pooladdr);
59+
ioctl_readwrite!(pf_begin_addrs, b'D', 51, pfvar::pfioc_pooladdr);
6060
// DIOCADDADDR
61-
ioctl!(readwrite pf_add_addr with b'D', 52; pfvar::pfioc_pooladdr);
61+
ioctl_readwrite!(pf_add_addr, b'D', 52, pfvar::pfioc_pooladdr);
6262
// DIOCXBEGIN
63-
ioctl!(readwrite pf_begin_trans with b'D', 81; pfvar::pfioc_trans);
63+
ioctl_readwrite!(pf_begin_trans, b'D', 81, pfvar::pfioc_trans);
6464
// DIOCXCOMMIT
65-
ioctl!(readwrite pf_commit_trans with b'D', 82; pfvar::pfioc_trans);
65+
ioctl_readwrite!(pf_commit_trans, b'D', 82, pfvar::pfioc_trans);

src/macros.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
pub const IOCTL_ERROR: i32 = -1;
10-
119
/// Macro for taking an expression with an ioctl call, perform it and return a Rust ´Result´.
1210
macro_rules! ioctl_guard {
1311
($func:expr) => {
1412
ioctl_guard!($func, libc::EEXIST)
1513
};
1614
($func:expr, $already_active:expr) => {
17-
if unsafe { $func } == $crate::macros::IOCTL_ERROR {
18-
let io_error = ::std::io::Error::last_os_error();
19-
let error_code = io_error
20-
.raw_os_error()
21-
.expect("Errors created with last_os_error should have errno");
22-
let mut err = Err($crate::ErrorKind::IoctlError(io_error).into());
15+
// nix::ioctl calls return error numbers out of box.
16+
if let nix::Result::Err(errno) = unsafe { $func } {
17+
let error_code = errno as i32;
18+
let mut err = Err($crate::ErrorKind::IoctlError(std::io::Error::from_raw_os_error(error_code)).into());
2319
if error_code == $already_active {
2420
err = err.chain_err(|| $crate::ErrorKind::StateAlreadyActive);
2521
}

0 commit comments

Comments
 (0)