diff --git a/Cargo.lock b/Cargo.lock index ca09fe1..7597ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,39 +2,53 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "assert_matches" -version = "1.1.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e772942dccdf11b368c31e044e4fca9189f80a773d2f0808379de65894cbf57" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" [[package]] name = "backtrace" -version = "0.3.5" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebbbf59b1c43eefa8c3ede390fcc36820b4999f7914104015be25025e0d62af2" +checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" dependencies = [ - "backtrace-sys", - "cfg-if 0.1.0", + "addr2line", + "cc", + "cfg-if", "libc", + "miniz_oxide", + "object", "rustc-demangle", - "winapi", ] [[package]] -name = "backtrace-sys" -version = "0.1.3" +name = "bitflags" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3216d6e2b2c36c648a78afab0fdcb124d5365f7eb9b0895eab395549d76280d2" -dependencies = [ - "libc", -] +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "cfg-if" -version = "0.1.0" +name = "cc" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" [[package]] name = "cfg-if" @@ -63,7 +77,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn", + "syn 1.0.109", ] [[package]] @@ -74,7 +88,7 @@ checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" dependencies = [ "darling_core", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -87,7 +101,7 @@ dependencies = [ "derive_builder_core", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -99,7 +113,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn", + "syn 1.0.109", ] [[package]] @@ -114,9 +128,9 @@ dependencies = [ [[package]] name = "fnv" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "getrandom" @@ -124,22 +138,22 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "libc", "wasi", ] [[package]] -name = "ident_case" -version = "1.0.0" +name = "gimli" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c9826188e666f2ed92071d2dadef6edc430b11b158b5b2b3f4babbcc891eaaa" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" [[package]] -name = "ioctl-sys" -version = "0.8.0" +name = "ident_case" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bd11f3a29434026f5ff98c730b668ba74b1033637b8817940b54d040696133c" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "ipnetwork" @@ -156,6 +170,41 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + +[[package]] +name = "miniz_oxide" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" +dependencies = [ + "adler", +] + +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + +[[package]] +name = "object" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" +dependencies = [ + "memchr", +] + [[package]] name = "pfctl" version = "0.4.6" @@ -163,71 +212,96 @@ dependencies = [ "assert_matches", "derive_builder", "error-chain", - "ioctl-sys", "ipnetwork", "libc", + "nix", "scopeguard", "uuid", ] [[package]] name = "proc-macro2" -version = "1.0.0" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19f287c234c9b2d0308d692dee5c449c1a171167a6f8150f7cf2a49d8fd96967" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.0" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab938ebe6f1c82426b5fb82eaf10c3e3028c53deaa3fbe38f5904b37cf4d767" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "rustc-demangle" -version = "0.1.4" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3058a43ada2c2d0b92b3ae38007a2d0fa5e9db971be260e0171408a4ff471c95" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "scopeguard" -version = "1.0.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.0" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "369633cfe0f0bde1dfc037fb6c5a329d46586a31f981bed14d87487a3439ae37" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", +] [[package]] name = "strsim" -version = "0.9.0" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7be23478587f30ca7b4b423b6bee7baf5b6986c1e511bf1904a984cb6105621" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" [[package]] name = "syn" -version = "1.0.1" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "863ecbce06044c8380458360b4146d7372edadfedd77f120ba8c193da427b708" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] -name = "unicode-xid" -version = "0.2.0" +name = "unicode-ident" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "uuid" @@ -249,25 +323,3 @@ name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "winapi" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 4c2ac34..fca1440 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,8 @@ travis-ci = { repository = "mullvad/pfctl-rs" } [dependencies] error-chain = "0.12.4" -ioctl-sys = "0.8.0" +# Has to stick to 0.26.4 since versions >= 0.27 require Rust 1.65 and above. +nix = { version = "0.26.4", features = ["ioctl"], default-features = false } libc = "0.2.29" derive_builder = "0.9" ipnetwork = "0.20.0" diff --git a/src/ffi/mod.rs b/src/ffi/mod.rs index 70a6759..99c80c6 100644 --- a/src/ffi/mod.rs +++ b/src/ffi/mod.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ioctl_sys::ioctl; +use nix::{ioctl_none, ioctl_readwrite}; #[allow(non_camel_case_types)] #[allow(non_upper_case_globals)] @@ -32,34 +32,34 @@ pub mod tcp { // The documentation describing the order of calls and accepted parameters can be found at: // http://man.openbsd.org/pf.4 // DIOCSTART -ioctl!(none pf_start with b'D', 1); +ioctl_none!(pf_start, b'D', 1); // DIOCSTOP -ioctl!(none pf_stop with b'D', 2); +ioctl_none!(pf_stop, b'D', 2); // DIOCADDRULE -ioctl!(readwrite pf_add_rule with b'D', 4; pfvar::pfioc_rule); +ioctl_readwrite!(pf_add_rule, b'D', 4, pfvar::pfioc_rule); // DIOCGETRULES -ioctl!(readwrite pf_get_rules with b'D', 6; pfvar::pfioc_rule); +ioctl_readwrite!(pf_get_rules, b'D', 6, pfvar::pfioc_rule); // DIOCGETRULE -ioctl!(readwrite pf_get_rule with b'D', 7; pfvar::pfioc_rule); +ioctl_readwrite!(pf_get_rule, b'D', 7, pfvar::pfioc_rule); // DIOCCLRSTATES -ioctl!(readwrite pf_clear_states with b'D', 18; pfvar::pfioc_state_kill); +ioctl_readwrite!(pf_clear_states, b'D', 18, pfvar::pfioc_state_kill); // DIOCGETSTATUS -ioctl!(readwrite pf_get_status with b'D', 21; pfvar::pf_status); +ioctl_readwrite!(pf_get_status, b'D', 21, pfvar::pf_status); // DIOCGETSTATES -ioctl!(readwrite pf_get_states with b'D', 25; pfvar::pfioc_states); +ioctl_readwrite!(pf_get_states, b'D', 25, pfvar::pfioc_states); // DIOCCHANGERULE -ioctl!(readwrite pf_change_rule with b'D', 26; pfvar::pfioc_rule); +ioctl_readwrite!(pf_change_rule, b'D', 26, pfvar::pfioc_rule); // DIOCINSERTRULE -ioctl!(readwrite pf_insert_rule with b'D', 27; pfvar::pfioc_rule); +ioctl_readwrite!(pf_insert_rule, b'D', 27, pfvar::pfioc_rule); // DIOCDELETERULE -ioctl!(readwrite pf_delete_rule with b'D', 28; pfvar::pfioc_rule); +ioctl_readwrite!(pf_delete_rule, b'D', 28, pfvar::pfioc_rule); // DIOCKILLSTATES -ioctl!(readwrite pf_kill_states with b'D', 41; pfvar::pfioc_state_kill); +ioctl_readwrite!(pf_kill_states, b'D', 41, pfvar::pfioc_state_kill); // DIOCBEGINADDRS -ioctl!(readwrite pf_begin_addrs with b'D', 51; pfvar::pfioc_pooladdr); +ioctl_readwrite!(pf_begin_addrs, b'D', 51, pfvar::pfioc_pooladdr); // DIOCADDADDR -ioctl!(readwrite pf_add_addr with b'D', 52; pfvar::pfioc_pooladdr); +ioctl_readwrite!(pf_add_addr, b'D', 52, pfvar::pfioc_pooladdr); // DIOCXBEGIN -ioctl!(readwrite pf_begin_trans with b'D', 81; pfvar::pfioc_trans); +ioctl_readwrite!(pf_begin_trans, b'D', 81, pfvar::pfioc_trans); // DIOCXCOMMIT -ioctl!(readwrite pf_commit_trans with b'D', 82; pfvar::pfioc_trans); +ioctl_readwrite!(pf_commit_trans, b'D', 82, pfvar::pfioc_trans); diff --git a/src/macros.rs b/src/macros.rs index aa38f5f..f91407c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -6,20 +6,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -pub const IOCTL_ERROR: i32 = -1; - /// Macro for taking an expression with an ioctl call, perform it and return a Rust ´Result´. macro_rules! ioctl_guard { ($func:expr) => { ioctl_guard!($func, libc::EEXIST) }; ($func:expr, $already_active:expr) => { - if unsafe { $func } == $crate::macros::IOCTL_ERROR { - let io_error = ::std::io::Error::last_os_error(); - let error_code = io_error - .raw_os_error() - .expect("Errors created with last_os_error should have errno"); - let mut err = Err($crate::ErrorKind::IoctlError(io_error).into()); + // nix::ioctl calls return error numbers out of box. + if let nix::Result::Err(errno) = unsafe { $func } { + let error_code = errno as i32; + let mut err = Err($crate::ErrorKind::IoctlError(std::io::Error::from_raw_os_error(error_code)).into()); if error_code == $already_active { err = err.chain_err(|| $crate::ErrorKind::StateAlreadyActive); }