Skip to content

Commit

Permalink
Change error-chain to thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
RoDmitry committed May 2, 2024
1 parent 106e112 commit 6138559
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 89 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ edition = "2018"
[badges]
travis-ci = { repository = "mullvad/pfctl-rs" }


[dependencies]
derive_builder = "0.9"
errno = "0.2"
error-chain = "0.12"
ioctl-sys = "0.6.0"
libc = "0.2.29"
derive_builder = "0.9"
ipnetwork = "0.16"
libc = "0.2.29"
thiserror = "1"

[dev-dependencies]
assert_matches = "1.1.0"
uuid = { version = "0.8", features = ["v4"] }
error-chain = "0.12"
scopeguard = "1.0"
uuid = { version = "0.8", features = ["v4"] }
5 changes: 4 additions & 1 deletion examples/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ fn run() -> Result<()> {
// Try to enable the firewall. Equivalent to the CLI command "pfctl -e".
match pf.enable() {
Ok(_) => println!("Enabled PF"),
Err(pfctl::Error(pfctl::ErrorKind::StateAlreadyActive, _)) => (),
Err(pfctl::Error {
source: pfctl::ErrorSource::StateAlreadyActive(_),
..
}) => (),
err => err.chain_err(|| "Unable to enable PF")?,
}
Ok(())
Expand Down
136 changes: 96 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@
#![deny(rust_2018_idioms)]

#[macro_use]
pub extern crate error_chain;

use std::{
ffi::CStr,
fmt::Display,
fs::File,
mem,
os::unix::io::{AsRawFd, RawFd},
};
use thiserror::Error;

pub use ipnetwork;

Expand All @@ -92,40 +91,89 @@ pub use crate::ruleset::*;
mod transaction;
pub use crate::transaction::*;

mod errors {
error_chain! {
errors {
DeviceOpenError(s: &'static str) {
description("Unable to open PF device file")
display("Unable to open PF device file at '{}'", s)
}
InvalidArgument(s: &'static str) {
display("Invalid argument: {}", s)
}
StateAlreadyActive {
description("Target state is already active")
}
InvalidRuleCombination(s: String) {
description("Rule contains incompatible values")
display("Incompatible values in rule: {}", s)
}
AnchorDoesNotExist {
display("Anchor does not exist")
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ErrorSource {
#[error("Unable to open PF device file at {}", _0)]
DeviceOpen(&'static str, #[source] ::std::io::Error),

#[error("Lower port is greater than upper port.")]
LowerIsGreaterPort,

#[error("String does not fit destination")]
StrCopyNotFits,

#[error("String has null byte")]
StrCopyNullByte,

#[error("Target state is already active")]
StateAlreadyActive(#[source] ::std::io::Error),

#[error("Incompatible values in rule: {}", _0)]
InvalidRuleCombination(String),

#[error("Anchor does not exist")]
AnchorDoesNotExist,

#[error("Cstr not null terminated")]
CstrNotTerminated,

#[error("Ioctl Error")]
Ioctl(#[from] ::std::io::Error),
}

#[derive(Error, Debug)]
pub struct Error {
pub info: Option<ErrorInfo>,
#[source]
pub source: ErrorSource,
}

impl Error {
fn new(info: ErrorInfo, err: Error) -> Self {
Self {
info: Some(info),
source: err.source,
}
foreign_links {
IoctlError(::std::io::Error);
}
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(info) = self.info.as_ref() {
info.fmt(f)?;
}
Ok(())
}
}
pub use crate::errors::*;

/// Returns the given input result, except if it is an `Err` matching the given `ErrorKind`,
impl From<ErrorSource> for Error {
fn from(source: ErrorSource) -> Self {
Self { info: None, source }
}
}

#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ErrorInfo {
#[error("Invalid anchor name")]
InvalidAnchorName,

#[error("Incompatible interface name")]
IncompatibleInterfaceName,

#[error("Invalid route target")]
InvalidRouteTarget,
}

pub type Result<T> = ::std::result::Result<T, Error>;

/// Returns the given input result, except if it is an `Err` matching the given `Error`,
/// then it returns `Ok(())` instead, so the error is ignored.
macro_rules! ignore_error_kind {
($result:expr, $kind:pat) => {
match $result {
Err($crate::Error($kind, _)) => Ok(()),
Err($crate::Error { source: $kind, .. }) => Ok(()),
result => result,
}
};
Expand All @@ -148,7 +196,9 @@ use crate::conversion::*;

/// Internal function to safely compare Rust string with raw C string slice
fn compare_cstr_safe(s: &str, cchars: &[std::os::raw::c_char]) -> Result<bool> {
ensure!(cchars.iter().any(|&c| c == 0), "Not null terminated");
if !(cchars.iter().any(|&c| c == 0)) {
return Err(ErrorSource::CstrNotTerminated.into());
}
let cs = unsafe { CStr::from_ptr(cchars.as_ptr()) };
Ok(s.as_bytes() == cs.to_bytes())
}
Expand All @@ -174,7 +224,7 @@ impl PfCtl {
/// Same as `enable`, but `StateAlreadyActive` errors are supressed and exchanged for
/// `Ok(())`.
pub fn try_enable(&mut self) -> Result<()> {
ignore_error_kind!(self.enable(), ErrorKind::StateAlreadyActive)
ignore_error_kind!(self.enable(), crate::ErrorSource::StateAlreadyActive(_))
}

/// Tries to disable PF. If the firewall is already disabled it will return an
Expand All @@ -186,7 +236,7 @@ impl PfCtl {
/// Same as `disable`, but `StateAlreadyActive` errors are supressed and exchanged for
/// `Ok(())`.
pub fn try_disable(&mut self) -> Result<()> {
ignore_error_kind!(self.disable(), ErrorKind::StateAlreadyActive)
ignore_error_kind!(self.disable(), crate::ErrorSource::StateAlreadyActive(_))
}

/// Tries to determine if PF is enabled or not.
Expand All @@ -201,7 +251,7 @@ impl PfCtl {

pfioc_rule.rule.action = kind.into();
name.try_copy_to(&mut pfioc_rule.anchor_call[..])
.chain_err(|| ErrorKind::InvalidArgument("Invalid anchor name"))?;
.map_err(|e| Error::new(ErrorInfo::InvalidAnchorName, e))?;

ioctl_guard!(ffi::pf_insert_rule(self.fd(), &mut pfioc_rule))?;
Ok(())
Expand All @@ -210,7 +260,10 @@ impl PfCtl {
/// Same as `add_anchor`, but `StateAlreadyActive` errors are supressed and exchanged for
/// `Ok(())`.
pub fn try_add_anchor(&mut self, name: &str, kind: AnchorKind) -> Result<()> {
ignore_error_kind!(self.add_anchor(name, kind), ErrorKind::StateAlreadyActive)
ignore_error_kind!(
self.add_anchor(name, kind),
crate::ErrorSource::StateAlreadyActive(_)
)
}

pub fn remove_anchor(&mut self, name: &str, kind: AnchorKind) -> Result<()> {
Expand All @@ -224,7 +277,7 @@ impl PfCtl {
pub fn try_remove_anchor(&mut self, name: &str, kind: AnchorKind) -> Result<()> {
ignore_error_kind!(
self.remove_anchor(name, kind),
ErrorKind::AnchorDoesNotExist
crate::ErrorSource::AnchorDoesNotExist
)
}

Expand All @@ -236,7 +289,7 @@ impl PfCtl {
pfioc_rule.ticket = utils::get_ticket(self.fd(), &anchor, AnchorKind::Filter)?;
anchor
.try_copy_to(&mut pfioc_rule.anchor[..])
.chain_err(|| ErrorKind::InvalidArgument("Invalid anchor name"))?;
.map_err(|e| Error::new(ErrorInfo::InvalidAnchorName, e))?;
rule.try_copy_to(&mut pfioc_rule.rule)?;

pfioc_rule.action = ffi::pfvar::PF_CHANGE_ADD_TAIL as u32;
Expand Down Expand Up @@ -287,7 +340,7 @@ impl PfCtl {

/// Clear states created by rules in anchor.
/// Returns total number of removed states upon success, otherwise
/// ErrorKind::AnchorDoesNotExist if anchor does not exist.
/// Error::AnchorDoesNotExist if anchor does not exist.
pub fn clear_states(&mut self, anchor_name: &str, kind: AnchorKind) -> Result<u32> {
let pfsync_states = self.get_states()?;
if !pfsync_states.is_empty() {
Expand Down Expand Up @@ -317,7 +370,7 @@ impl PfCtl {
let mut pfioc_state_kill = unsafe { mem::zeroed::<ffi::pfvar::pfioc_state_kill>() };
interface
.try_copy_to(&mut pfioc_state_kill.psk_ifname)
.chain_err(|| ErrorKind::InvalidArgument("Incompatible interface name"))?;
.map_err(|e| Error::new(ErrorInfo::IncompatibleInterfaceName, e))?;
ioctl_guard!(ffi::pf_clear_states(self.fd(), &mut pfioc_state_kill))?;
// psk_af holds the number of killed states
Ok(pfioc_state_kill.psk_af as u32)
Expand All @@ -342,7 +395,7 @@ impl PfCtl {
/// The return value from closure is transparently passed to the caller.
///
/// - Returns Result<R> from call to closure on match.
/// - Returns `ErrorKind::AnchorDoesNotExist` on mismatch, the closure is not called in that
/// - Returns `Error::AnchorDoesNotExist` on mismatch, the closure is not called in that
/// case.
fn with_anchor_rule<F, R>(&self, name: &str, kind: AnchorKind, f: F) -> Result<R>
where
Expand All @@ -359,7 +412,7 @@ impl PfCtl {
return f(pfioc_rule);
}
}
bail!(ErrorKind::AnchorDoesNotExist);
Err(ErrorSource::AnchorDoesNotExist.into())
}

/// Returns global number of states created by all stateful rules (see keep_state)
Expand Down Expand Up @@ -419,7 +472,10 @@ mod tests {
let cchars: &[i8] = unsafe { mem::transmute(cstr.as_bytes()) };
assert_matches!(
compare_cstr_safe("Hello", cchars),
Err(ref e) if e.description() == "Not null terminated"
Err(Error {
source: ErrorSource::CstrNotTerminated,
..
})
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ macro_rules! ioctl_guard {
($func:expr, $already_active:expr) => {
if unsafe { $func } == $crate::macros::IOCTL_ERROR {
let ::errno::Errno(error_code) = ::errno::errno();
let io_error = ::std::io::Error::from_raw_os_error(error_code);
let mut err = Err($crate::ErrorKind::IoctlError(io_error).into());
let err = ::std::io::Error::from_raw_os_error(error_code);
if error_code == $already_active {
err = err.chain_err(|| $crate::ErrorKind::StateAlreadyActive);
Err($crate::ErrorSource::StateAlreadyActive(err).into())
} else {
Err($crate::ErrorSource::from(err).into())
}
err
} else {
Ok(()) as $crate::Result<()>
}
Expand Down
2 changes: 1 addition & 1 deletion src/pooladdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
conversion::{CopyTo, TryCopyTo},
ffi, Interface, Ip, Result,
};
use std::{mem, ptr, vec::Vec};
use std::{mem, ptr};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PoolAddr {
Expand Down
2 changes: 1 addition & 1 deletion src/rule/gid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub use super::uid::{Id, IdUnaryModifier};
pub use super::uid::Id;
use crate::{
conversion::TryCopyTo,
ffi::pfvar::{pf_rule_gid, PF_OP_NONE},
Expand Down
Loading

0 comments on commit 6138559

Please sign in to comment.