1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use crate::*;
use core::slice::from_raw_parts;
///A slice containing a single vlan header of a network package.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SingleVlanHeaderSlice<'a> {
slice: &'a [u8],
}
impl<'a> SingleVlanHeaderSlice<'a> {
///Creates a vlan header slice from a slice.
#[inline]
pub fn from_slice(slice: &'a [u8]) -> Result<SingleVlanHeaderSlice<'a>, err::LenError> {
//check length
if slice.len() < SingleVlanHeader::LEN {
return Err(err::LenError {
required_len: SingleVlanHeader::LEN,
len: slice.len(),
len_source: LenSource::Slice,
layer: err::Layer::VlanHeader,
layer_start_offset: 0,
});
}
//all done
Ok(SingleVlanHeaderSlice::<'a> {
// SAFETY:
// Safe as the slice length is checked beforehand to have
// at least the length of SingleVlanHeader::LEN (4)
slice: unsafe { from_raw_parts(slice.as_ptr(), SingleVlanHeader::LEN) },
})
}
/// Converts the given slice into a vlan header slice WITHOUT any
/// checks to ensure that the data present is an vlan header or that the
/// slice length is matching the header length.
///
/// If you are not sure what this means, use [`SingleVlanHeaderSlice::from_slice`]
/// instead.
///
/// # Safety
///
/// The caller must ensured that the given slice has the length of
/// [`SingleVlanHeader::LEN`]
#[inline]
pub(crate) unsafe fn from_slice_unchecked(slice: &[u8]) -> SingleVlanHeaderSlice {
SingleVlanHeaderSlice { slice }
}
/// Returns the slice containing the single vlan header
#[inline]
pub fn slice(&self) -> &'a [u8] {
self.slice
}
/// Read the "priority_code_point" field from the slice. This is a 3 bit number which refers to the IEEE 802.1p class of service and maps to the frame priority level.
#[inline]
pub fn priority_code_point(&self) -> VlanPcp {
unsafe {
// SAFETY: Safe as slice len checked in constructor to be at least 4 &
// the bitmask guarantees values does not exceed 0b0000_0111.
VlanPcp::new_unchecked((*self.slice.get_unchecked(0) >> 5) & 0b0000_0111)
}
}
/// Read the "drop_eligible_indicator" flag from the slice. Indicates that the frame may be dropped under the presence of congestion.
#[inline]
pub fn drop_eligible_indicator(&self) -> bool {
// SAFETY:
// Slice len checked in constructor to be at least 4.
unsafe { 0 != (*self.slice.get_unchecked(0) & 0x10) }
}
/// Reads the 12 bits "vland identifier" field from the slice.
#[inline]
pub fn vlan_identifier(&self) -> VlanId {
// SAFETY:
// Slice len checked in constructor to be at least 4 &
// value and the value is guaranteed not to exceed
// 0b0000_1111_1111_1111 as the upper bits have been
// bitmasked out.
unsafe {
VlanId::new_unchecked(u16::from_be_bytes([
*self.slice.get_unchecked(0) & 0b0000_1111,
*self.slice.get_unchecked(1),
]))
}
}
/// Read the "Tag protocol identifier" field from the slice. Refer to the "EtherType" for a list of possible supported values.
#[inline]
pub fn ether_type(&self) -> EtherType {
// SAFETY:
// Slice len checked in constructor to be at least 4.
EtherType(unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(2)) })
}
/// Decode all the fields and copy the results to a SingleVlanHeader struct
#[inline]
pub fn to_header(&self) -> SingleVlanHeader {
SingleVlanHeader {
pcp: self.priority_code_point(),
drop_eligible_indicator: self.drop_eligible_indicator(),
vlan_id: self.vlan_identifier(),
ether_type: self.ether_type(),
}
}
}
#[cfg(test)]
mod test {
use crate::{test_gens::*, *};
use alloc::{format, vec::Vec};
use proptest::prelude::*;
proptest! {
#[test]
fn from_slice(
input in vlan_single_any(),
dummy_data in proptest::collection::vec(any::<u8>(), 0..20)
) {
// serialize
let mut buffer: Vec<u8> = Vec::with_capacity(input.header_len() + dummy_data.len());
input.write(&mut buffer).unwrap();
buffer.extend(&dummy_data[..]);
// normal
{
let slice = SingleVlanHeaderSlice::from_slice(&buffer).unwrap();
assert_eq!(slice.slice(), &buffer[..4]);
}
// slice length to small
for len in 0..4 {
assert_eq!(
SingleVlanHeaderSlice::from_slice(&buffer[..len])
.unwrap_err(),
err::LenError{
required_len: 4,
len: len,
len_source: LenSource::Slice,
layer: err::Layer::VlanHeader,
layer_start_offset: 0,
}
);
}
}
}
proptest! {
#[test]
fn getters(input in vlan_single_any()) {
let bytes = input.to_bytes();
let slice = SingleVlanHeaderSlice::from_slice(&bytes).unwrap();
assert_eq!(input.pcp, slice.priority_code_point());
assert_eq!(input.drop_eligible_indicator, slice.drop_eligible_indicator());
assert_eq!(input.vlan_id, slice.vlan_identifier());
assert_eq!(input.ether_type, slice.ether_type());
}
}
proptest! {
#[test]
fn to_header(input in vlan_single_any()) {
let bytes = input.to_bytes();
let slice = SingleVlanHeaderSlice::from_slice(&bytes).unwrap();
assert_eq!(input, slice.to_header());
}
}
proptest! {
#[test]
fn clone_eq(input in vlan_single_any()) {
let bytes = input.to_bytes();
let slice = SingleVlanHeaderSlice::from_slice(&bytes).unwrap();
assert_eq!(slice, slice.clone());
}
}
proptest! {
#[test]
fn dbg(input in vlan_single_any()) {
let bytes = input.to_bytes();
let slice = SingleVlanHeaderSlice::from_slice(&bytes).unwrap();
assert_eq!(
&format!(
"SingleVlanHeaderSlice {{ slice: {:?} }}",
slice.slice(),
),
&format!("{:?}", slice)
);
}
}
}