diff --git a/src/lib.rs b/src/lib.rs index 01cf1dd4..12f50291 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,8 @@ pub use self::address_family_freebsd::AddressFamily; target_os = "freebsd", )))] mod address_family_fallback; +mod net; + #[cfg(not(any( target_os = "linux", target_os = "fuchsia", diff --git a/src/tc/filters/arp.rs b/src/net/arp.rs similarity index 97% rename from src/tc/filters/arp.rs rename to src/net/arp.rs index 9f15f05b..2d600f7e 100644 --- a/src/tc/filters/arp.rs +++ b/src/net/arp.rs @@ -1,7 +1,6 @@ /// List from [iana.org][1] /// /// [1]: https://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml -use crate::tc::filters::ethernet::Mac; const RESERVED: u8 = 0; const REQUEST: u8 = 1; @@ -136,8 +135,3 @@ impl From for u8 { *value.as_ref() } } - -pub type Sha = Mac; -pub type ShaMask = Mac; -pub type Tha = Mac; -pub type ThaMask = Mac; diff --git a/src/tc/filters/ethernet.rs b/src/net/ethernet.rs similarity index 98% rename from src/tc/filters/ethernet.rs rename to src/net/ethernet.rs index c42ea2a1..4eeb4386 100644 --- a/src/tc/filters/ethernet.rs +++ b/src/net/ethernet.rs @@ -1,5 +1,3 @@ -use anyhow::Error; - #[derive(Debug, PartialEq, Eq, Clone)] pub struct Mac([u8; 6]); pub type MacMask = Mac; @@ -310,7 +308,7 @@ impl From for u16 { pub struct VlanId(u16); impl VlanId { - pub fn try_new(id: u16) -> Result { + pub fn try_new(id: u16) -> Result { if id >= 4096 { return Err(anyhow::anyhow!("VLAN ID must be less than 4096")); } @@ -319,7 +317,7 @@ impl VlanId { } impl TryFrom for VlanId { - type Error = Error; + type Error = anyhow::Error; fn try_from(id: u16) -> Result { Self::try_new(id) @@ -342,7 +340,7 @@ impl From for u16 { pub struct VlanPrio(u8); impl VlanPrio { - pub fn try_new(prio: u8) -> Result { + pub fn try_new(prio: u8) -> Result { if prio > Self::HIGHEST.into() { return Err(anyhow::anyhow!("VLAN priority must be less than 8")); } @@ -354,7 +352,7 @@ impl VlanPrio { } impl TryFrom for VlanPrio { - type Error = Error; + type Error = anyhow::Error; fn try_from(prio: u8) -> Result { Self::try_new(prio) diff --git a/src/tc/filters/icmpv4.rs b/src/net/icmpv4.rs similarity index 98% rename from src/tc/filters/icmpv4.rs rename to src/net/icmpv4.rs index 21bce945..da3c0285 100644 --- a/src/tc/filters/icmpv4.rs +++ b/src/net/icmpv4.rs @@ -1,11 +1,3 @@ -#![deny( - clippy::all, - clippy::pedantic, - clippy::unwrap_used, - clippy::expect_used, - clippy::panic -)] - /// Lists sourced from [iana.org][1] /// /// [1]: https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml @@ -508,16 +500,16 @@ impl From for u8 { #[non_exhaustive] #[repr(u8)] pub enum TimeExceeded { - TimeToLiveExceededInTransit = 0, - FragmentReassemblyTimeExceeded = 1, + TtlExceededInTransit = 0, + FragmentReassembly = 1, Other(u8), } impl AsRef for TimeExceeded { fn as_ref(&self) -> &u8 { match self { - TimeExceeded::TimeToLiveExceededInTransit => &0, - TimeExceeded::FragmentReassemblyTimeExceeded => &1, + TimeExceeded::TtlExceededInTransit => &0, + TimeExceeded::FragmentReassembly => &1, TimeExceeded::Other(x) => x, } } @@ -526,8 +518,8 @@ impl AsRef for TimeExceeded { impl From for TimeExceeded { fn from(value: u8) -> Self { match value { - 0 => TimeExceeded::TimeToLiveExceededInTransit, - 1 => TimeExceeded::FragmentReassemblyTimeExceeded, + 0 => TimeExceeded::TtlExceededInTransit, + 1 => TimeExceeded::FragmentReassembly, x => TimeExceeded::Other(x), } } @@ -1268,6 +1260,3 @@ impl AsRef for Code { } } } - -pub type TypeMask = u8; -pub type CodeMask = u8; diff --git a/src/tc/filters/icmpv6.rs b/src/net/icmpv6.rs similarity index 99% rename from src/tc/filters/icmpv6.rs rename to src/net/icmpv6.rs index 84fa03a4..5c0babd1 100644 --- a/src/tc/filters/icmpv6.rs +++ b/src/net/icmpv6.rs @@ -232,6 +232,4 @@ impl From for u8 { } } -pub type TypeMask = u8; pub type Code = u8; -pub type CodeMask = u8; diff --git a/src/net/mod.rs b/src/net/mod.rs new file mode 100644 index 00000000..f8079af7 --- /dev/null +++ b/src/net/mod.rs @@ -0,0 +1,15 @@ +// General purpose networking abstractions. +#![forbid(unsafe_code)] +#![deny( + clippy::all, + clippy::pedantic, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic +)] + +pub mod arp; +pub mod ethernet; +pub mod icmpv4; +pub mod icmpv6; +pub mod mpls; diff --git a/src/net/mpls.rs b/src/net/mpls.rs new file mode 100644 index 00000000..f8563107 --- /dev/null +++ b/src/net/mpls.rs @@ -0,0 +1,69 @@ +use std::convert::TryFrom; +use anyhow::Error; + +#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)] +#[repr(transparent)] +pub struct Label(u32); + +/// Should we add handling for reserved labels as per [RFC 3032][1] (see page +/// 4)? +/// +/// [1]: https://www.iana.org/assignments/mpls-label-values/mpls-label-values.xhtml +impl Label { + pub fn try_new(label: u32) -> Result { + if label > 0xFFFFF { + Err(Error::msg("MPLS label must be less than 0xFFFFF"))?; + } + Ok(Self(label)) + } +} + +impl TryFrom for Label { + type Error = Error; + + fn try_from(label: u32) -> Result { + Self::try_new(label) + } +} + +impl From