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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
use crate::*;
/// Slice containing laxly parsed the network headers & payloads (e.g. IPv4, IPv6, ARP).
///
/// Compared to the normal [`NetSlice`] this slice allows the
/// payload to be incomplete/cut off and errors to be present in
/// the IpPayload.
///
/// The main usecases for "laxly" parsed slices are are:
///
/// * Parsing packets that have been cut off. This is, for example, useful to
/// parse packets returned via ICMP as these usually only contain the start.
/// * Parsing packets where the `total_len` (for IPv4) or `payload_len` (for IPv6)
/// have not yet been set. This can be useful when parsing packets which have
/// been recorded in a layer before the length field was set (e.g. before the
/// operating system set the length fields).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LaxNetSlice<'a> {
/// The ipv4 header & the decoded extension headers.
Ipv4(LaxIpv4Slice<'a>),
/// The ipv6 header & the decoded extension headers.
Ipv6(LaxIpv6Slice<'a>),
}
impl<'a> LaxNetSlice<'a> {
/// Returns a reference to ip payload if the net slice contains
/// an ipv4 or ipv6 slice.
#[inline]
pub fn ip_payload_ref(&self) -> Option<&LaxIpPayloadSlice<'a>> {
match self {
LaxNetSlice::Ipv4(s) => Some(&s.payload),
LaxNetSlice::Ipv6(s) => Some(&s.payload),
}
}
}
impl<'a> From<LaxIpSlice<'a>> for LaxNetSlice<'a> {
#[inline]
fn from(value: LaxIpSlice<'a>) -> LaxNetSlice<'a> {
match value {
LaxIpSlice::Ipv4(ipv4) => LaxNetSlice::Ipv4(ipv4),
LaxIpSlice::Ipv6(ipv6) => LaxNetSlice::Ipv6(ipv6),
}
}
}
impl<'a> From<LaxIpv4Slice<'a>> for LaxNetSlice<'a> {
#[inline]
fn from(value: LaxIpv4Slice<'a>) -> LaxNetSlice<'a> {
LaxNetSlice::Ipv4(value)
}
}
impl<'a> From<LaxIpv6Slice<'a>> for LaxNetSlice<'a> {
#[inline]
fn from(value: LaxIpv6Slice<'a>) -> LaxNetSlice<'a> {
LaxNetSlice::Ipv6(value)
}
}
#[cfg(test)]
mod tests {
use crate::*;
use alloc::{format, vec::Vec};
#[test]
fn debug() {
let bytes = Ipv6Header {
next_header: IpNumber::UDP,
..Default::default()
}
.to_bytes();
let s = LaxIpv6Slice::from_slice(&bytes).unwrap().0;
let n = LaxNetSlice::Ipv6(s.clone());
assert_eq!(format!("{n:?}"), format!("Ipv6({s:?})"));
}
#[test]
fn clone_eq() {
let bytes = Ipv6Header {
next_header: IpNumber::UDP,
..Default::default()
}
.to_bytes();
let s = LaxNetSlice::Ipv6(LaxIpv6Slice::from_slice(&bytes).unwrap().0);
assert_eq!(s, s.clone())
}
#[test]
fn ip_payload_ref() {
// ipv4
{
let payload = [1, 2, 3, 4];
let bytes = {
let mut bytes = Vec::with_capacity(Ipv4Header::MIN_LEN + 4);
bytes.extend_from_slice(
&(Ipv4Header {
total_len: Ipv4Header::MIN_LEN_U16 + 4,
protocol: IpNumber::UDP,
..Default::default()
})
.to_bytes(),
);
bytes.extend_from_slice(&payload);
bytes
};
let s = LaxNetSlice::Ipv4(LaxIpv4Slice::from_slice(&bytes).unwrap().0);
assert_eq!(
s.ip_payload_ref(),
Some(&LaxIpPayloadSlice {
ip_number: IpNumber::UDP,
fragmented: false,
len_source: LenSource::Ipv4HeaderTotalLen,
payload: &payload,
incomplete: false,
})
);
}
// ipv6
{
let payload = [1, 2, 3, 4];
let bytes = {
let mut bytes = Vec::with_capacity(Ipv6Header::LEN + 4);
bytes.extend_from_slice(
&(Ipv6Header {
next_header: IpNumber::UDP,
payload_length: 4,
..Default::default()
})
.to_bytes(),
);
bytes.extend_from_slice(&payload);
bytes
};
let s = LaxNetSlice::Ipv6(LaxIpv6Slice::from_slice(&bytes).unwrap().0);
assert_eq!(
s.ip_payload_ref(),
Some(&LaxIpPayloadSlice {
ip_number: IpNumber::UDP,
fragmented: false,
len_source: LenSource::Ipv6HeaderPayloadLen,
payload: &payload,
incomplete: false,
})
);
}
}
#[test]
fn from() {
// IpSlice::Ipv4
{
let payload = [1, 2, 3, 4];
let bytes = {
let mut bytes = Vec::with_capacity(Ipv4Header::MIN_LEN + 4);
bytes.extend_from_slice(
&(Ipv4Header {
total_len: Ipv4Header::MIN_LEN_U16 + 4,
protocol: IpNumber::UDP,
..Default::default()
})
.to_bytes(),
);
bytes.extend_from_slice(&payload);
bytes
};
let i = LaxIpv4Slice::from_slice(&bytes).unwrap().0;
let actual: LaxNetSlice = LaxIpSlice::Ipv4(i.clone()).into();
assert_eq!(LaxNetSlice::Ipv4(i.clone()), actual);
}
// LaxIpv4Slice
{
let payload = [1, 2, 3, 4];
let bytes = {
let mut bytes = Vec::with_capacity(Ipv4Header::MIN_LEN + 4);
bytes.extend_from_slice(
&(Ipv4Header {
total_len: Ipv4Header::MIN_LEN_U16 + 4,
protocol: IpNumber::UDP,
..Default::default()
})
.to_bytes(),
);
bytes.extend_from_slice(&payload);
bytes
};
let i = LaxIpv4Slice::from_slice(&bytes).unwrap().0;
let actual: LaxNetSlice = i.clone().into();
assert_eq!(LaxNetSlice::Ipv4(i.clone()), actual);
}
// IpSlice::Ipv6
{
let payload = [1, 2, 3, 4];
let bytes = {
let mut bytes = Vec::with_capacity(Ipv6Header::LEN + 4);
bytes.extend_from_slice(
&(Ipv6Header {
next_header: IpNumber::UDP,
payload_length: 4,
..Default::default()
})
.to_bytes(),
);
bytes.extend_from_slice(&payload);
bytes
};
let i = LaxIpv6Slice::from_slice(&bytes).unwrap().0;
let actual: LaxNetSlice = i.clone().into();
assert_eq!(LaxNetSlice::Ipv6(i.clone()), actual);
}
}
}