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
use crate::*;

/// A slice containing the link layer header (currently only Ethernet II and
/// SLL are supported).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LinkSlice<'a> {
    /// A slice containing an Ethernet II header.
    Ethernet2(Ethernet2Slice<'a>),

    /// A slice containing a Linux Cooked Capture v1 (SLL) header.
    LinuxSll(LinuxSllSlice<'a>),

    /// Ether payload without header.
    EtherPayload(EtherPayloadSlice<'a>),

    /// Sll payload without header.
    LinuxSllPayload(LinuxSllPayloadSlice<'a>),
}

impl<'a> LinkSlice<'a> {
    /// Convert the link slice to a header
    pub fn to_header(&self) -> Option<LinkHeader> {
        use LinkSlice::*;
        match self {
            Ethernet2(slice) => Some(LinkHeader::Ethernet2(slice.to_header())),
            LinuxSll(slice) => Some(LinkHeader::LinuxSll(slice.to_header())),
            EtherPayload(_) => None,
            LinuxSllPayload(_) => None,
        }
    }

    /// Returns the link layer ether payload (slice + ether type number).
    pub fn ether_payload(&self) -> Option<EtherPayloadSlice<'a>> {
        use LinkSlice::*;
        match self {
            Ethernet2(s) => Some(s.payload().clone()),
            LinuxSll(s) => Some(EtherPayloadSlice::try_from(s.payload()).ok()?.clone()),
            EtherPayload(p) => Some(p.clone()),
            LinuxSllPayload(p) => Some(EtherPayloadSlice::try_from(p.clone()).ok()?),
        }
    }

    /// Returns the link layer sll payload (slice + link layer protocol type).
    pub fn sll_payload(&self) -> LinuxSllPayloadSlice<'a> {
        use LinkSlice::*;
        match self {
            Ethernet2(s) => LinuxSllPayloadSlice::from(s.payload().clone()),
            LinuxSll(s) => s.payload().clone(),
            EtherPayload(p) => LinuxSllPayloadSlice::from(p.clone()),
            LinuxSllPayload(p) => p.clone(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_gens::*;
    use alloc::{format, vec::Vec};
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn debug_clone_eq(ref eth in ethernet_2_unknown()) {
            let bytes = eth.to_bytes();
            let e = Ethernet2Slice::from_slice_without_fcs(&bytes).unwrap();
            let slice = LinkSlice::Ethernet2(
                e.clone()
            );

            // clone & eq
            assert_eq!(slice.clone(), slice);

            // debug
            assert_eq!(
                format!("{:?}", slice),
                format!("Ethernet2({:?})", e),
            );
        }
    }

    proptest! {
        #[test]
        fn to_header(
            ref eth in ethernet_2_unknown(),
            ref linux_sll in linux_sll_any()
        ) {
            {
                let bytes = eth.to_bytes();
                let slice = LinkSlice::Ethernet2(
                    Ethernet2Slice::from_slice_without_fcs(&bytes).unwrap()
                );
                assert_eq!(
                    slice.to_header(),
                    Some(LinkHeader::Ethernet2(eth.clone()))
                );
            }
            {
                let bytes = linux_sll.to_bytes();
                let slice = LinkSlice::LinuxSll(
                    LinuxSllSlice::from_slice(&bytes).unwrap()
                );
                assert_eq!(
                    slice.to_header(),
                    Some(LinkHeader::LinuxSll(linux_sll.clone()))
                );
            }
            {
                let slice = LinkSlice::EtherPayload(EtherPayloadSlice {
                    ether_type: ether_type::IPV4,
                    payload: &[]
                });
                assert_eq!(
                    slice.to_header(),
                    None
                );
            }
            {
                let slice = LinkSlice::LinuxSllPayload(LinuxSllPayloadSlice {
                    protocol_type: LinuxSllProtocolType::EtherType(ether_type::IPV4),
                    payload: &[]
                });
                assert_eq!(
                    slice.to_header(),
                    None
                );
            }
        }
    }

    proptest! {
        #[test]
        fn ether_payload(
            ref eth in ethernet_2_unknown(),
            ref linux_sll in linux_sll_any()
        ) {
            let p = [1,2,3,4];
            {
                let mut bytes = Vec::with_capacity(Ethernet2Header::LEN + p.len());
                bytes.extend_from_slice(&eth.to_bytes());
                bytes.extend_from_slice(&p);
                let slice = LinkSlice::Ethernet2(
                    Ethernet2Slice::from_slice_without_fcs(&bytes).unwrap()
                );
                assert_eq!(
                    slice.ether_payload().unwrap(),
                    EtherPayloadSlice{ ether_type: eth.ether_type, payload: &p }
                );
            }
            {
                let slice = LinkSlice::EtherPayload(EtherPayloadSlice {
                    ether_type: eth.ether_type,
                    payload: &p
                });
                assert_eq!(
                    slice.ether_payload().unwrap(),
                    EtherPayloadSlice{ ether_type: eth.ether_type, payload: &p }
                );
            }
            {
                let mut bytes = Vec::with_capacity(LinuxSllHeader::LEN + p.len());
                bytes.extend_from_slice(&linux_sll.to_bytes());
                bytes.extend_from_slice(&p);
                let slice = LinkSlice::LinuxSll(
                    LinuxSllSlice::from_slice(&bytes).unwrap()
                );
                match linux_sll.protocol_type {
                    LinuxSllProtocolType::EtherType(EtherType(v)) | LinuxSllProtocolType::LinuxNonstandardEtherType(LinuxNonstandardEtherType(v)) => { assert_eq!(
                            slice.ether_payload().unwrap(),
                            EtherPayloadSlice{ ether_type: EtherType(v), payload: &p }
                    );}
                    _ => { assert!(slice.ether_payload().is_none());}
                }
            }
            {
                let slice = LinkSlice::LinuxSllPayload(LinuxSllPayloadSlice {
                    protocol_type: linux_sll.protocol_type,
                    payload: &p
                });
                match linux_sll.protocol_type {
                    LinuxSllProtocolType::EtherType(EtherType(v)) | LinuxSllProtocolType::LinuxNonstandardEtherType(LinuxNonstandardEtherType(v)) => { assert_eq!(
                        slice.ether_payload().unwrap(),
                            EtherPayloadSlice{ ether_type: EtherType(v), payload: &p }
                    );}
                    _ => { assert!(slice.ether_payload().is_none());}
                }
            }
        }
    }
}