pfctl/rule/
interface.rs

1// Copyright 2025 Mullvad VPN AB.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::conversion::TryCopyTo;
10use crate::{Error, ErrorInternal};
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub struct InterfaceName(String);
14
15impl AsRef<str> for InterfaceName {
16    fn as_ref(&self) -> &str {
17        self.0.as_ref()
18    }
19}
20
21#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
22pub enum Interface {
23    #[default]
24    Any,
25    Name(InterfaceName),
26}
27
28impl<T: AsRef<str>> From<T> for Interface {
29    fn from(name: T) -> Self {
30        Interface::Name(InterfaceName(name.as_ref().to_owned()))
31    }
32}
33
34impl TryCopyTo<[i8]> for Interface {
35    type Error = crate::Error;
36
37    fn try_copy_to(&self, dst: &mut [i8]) -> Result<(), Self::Error> {
38        match *self {
39            Interface::Any => "",
40            Interface::Name(InterfaceName(ref name)) => &name[..],
41        }
42        .try_copy_to(dst)
43        .map_err(|reason| Error::from(ErrorInternal::InvalidInterfaceName(reason)))
44    }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq, Hash)]
48#[repr(i32)]
49pub enum InterfaceFlags {
50    /// Set or clear the skip flag on an interface.
51    /// This is equivalent to PFI_IFLAG_SKIP.
52    Skip = 0x0100,
53}