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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::{err::Layer, err::SliceWriteSpaceError, *};

/// Ethernet II header.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Ethernet2Header {
    /// Source MAC Address
    pub source: [u8; 6],
    /// Destination MAC Address
    pub destination: [u8; 6],
    /// Protocol present after the ethernet2 header.
    pub ether_type: EtherType,
}

impl Ethernet2Header {
    /// Serialized size of an Ethernet2 header in bytes/octets.
    pub const LEN: usize = 14;

    /// Deprecated use [`Ethernet2Header::LEN`] instead.
    #[deprecated(since = "0.14.0", note = "Use `Ethernet2Header::LEN` instead")]
    pub const SERIALIZED_SIZE: usize = Ethernet2Header::LEN;

    /// Deprecated use [`Ethernet2Header::from_slice`] instead.
    #[deprecated(since = "0.10.1", note = "Use Ethernet2Header::from_slice instead.")]
    #[inline]
    pub fn read_from_slice(slice: &[u8]) -> Result<(Ethernet2Header, &[u8]), err::LenError> {
        Ethernet2Header::from_slice(slice)
    }

    /// Read an Ethernet2Header from a slice and return the header & unused parts of the slice.
    #[inline]
    pub fn from_slice(slice: &[u8]) -> Result<(Ethernet2Header, &[u8]), err::LenError> {
        Ok((
            Ethernet2HeaderSlice::from_slice(slice)?.to_header(),
            &slice[Ethernet2Header::LEN..],
        ))
    }

    /// Read an Ethernet2Header from a static sized byte array.
    #[inline]
    pub fn from_bytes(bytes: [u8; 14]) -> Ethernet2Header {
        Ethernet2Header {
            destination: [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]],
            source: [bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11]],
            ether_type: EtherType(u16::from_be_bytes([bytes[12], bytes[13]])),
        }
    }

    /// Reads an Ethernet-II header from the current position of the read argument.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn read<T: std::io::Read + std::io::Seek + Sized>(
        reader: &mut T,
    ) -> Result<Ethernet2Header, std::io::Error> {
        let buffer = {
            let mut buffer = [0; Ethernet2Header::LEN];
            reader.read_exact(&mut buffer)?;
            buffer
        };

        Ok(
            // SAFETY: Safe as the buffer contains exactly the needed Ethernet2Header::LEN bytes.
            unsafe { Ethernet2HeaderSlice::from_slice_unchecked(&buffer) }.to_header(),
        )
    }

    /// Serialize the header to a given slice. Returns the unused part of the slice.
    pub fn write_to_slice<'a>(
        &self,
        slice: &'a mut [u8],
    ) -> Result<&'a mut [u8], SliceWriteSpaceError> {
        // length check
        if slice.len() < Ethernet2Header::LEN {
            Err(SliceWriteSpaceError {
                required_len: Ethernet2Header::LEN,
                len: slice.len(),
                layer: Layer::Ethernet2Header,
                layer_start_offset: 0,
            })
        } else {
            slice[..Ethernet2Header::LEN].copy_from_slice(&self.to_bytes());
            Ok(&mut slice[Ethernet2Header::LEN..])
        }
    }

    /// Writes a given Ethernet-II header to the current position of the write argument.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    #[inline]
    pub fn write<T: std::io::Write + Sized>(&self, writer: &mut T) -> Result<(), std::io::Error> {
        writer.write_all(&self.to_bytes())
    }

    /// Length of the serialized header in bytes.
    #[inline]
    pub fn header_len(&self) -> usize {
        14
    }

    /// Returns the serialized form of the header as a statically
    /// sized byte array.
    #[inline]
    pub fn to_bytes(&self) -> [u8; 14] {
        let ether_type_be = self.ether_type.0.to_be_bytes();
        [
            self.destination[0],
            self.destination[1],
            self.destination[2],
            self.destination[3],
            self.destination[4],
            self.destination[5],
            self.source[0],
            self.source[1],
            self.source[2],
            self.source[3],
            self.source[4],
            self.source[5],
            ether_type_be[0],
            ether_type_be[1],
        ]
    }
}

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

    #[test]
    fn default() {
        let e: Ethernet2Header = Default::default();
        assert_eq!([0u8; 6], e.source);
        assert_eq!([0u8; 6], e.destination);
        assert_eq!(EtherType(0), e.ether_type);
    }

    proptest! {
        #[test]
        fn from_slice(
            input in ethernet_2_any(),
            dummy_data in proptest::collection::vec(any::<u8>(), 0..20)
        ) {
            // serialize
            let mut buffer: Vec<u8> = Vec::with_capacity(14 + dummy_data.len());
            input.write(&mut buffer).unwrap();
            buffer.extend(&dummy_data[..]);

            // calls with a valid result
            {
                let (result, rest) = Ethernet2Header::from_slice(&buffer[..]).unwrap();
                assert_eq!(input, result);
                assert_eq!(&buffer[14..], rest);
            }
            #[allow(deprecated)]
            {
                let (result, rest) = Ethernet2Header::read_from_slice(&buffer[..]).unwrap();
                assert_eq!(input, result);
                assert_eq!(&buffer[14..], rest);
            }

            // call with not enough data in the slice
            for len in 0..=13 {
                assert_eq!(
                    Ethernet2Header::from_slice(&buffer[..len]),
                    Err(err::LenError{
                        required_len: Ethernet2Header::LEN,
                        len: len,
                        len_source: LenSource::Slice,
                        layer: err::Layer::Ethernet2Header,
                        layer_start_offset: 0,
                    })
                );
            }
        }
    }

    proptest! {
        #[test]
        fn from_bytes(input in ethernet_2_any()) {
            assert_eq!(
                input,
                Ethernet2Header::from_bytes(input.to_bytes())
            );
        }
    }

    proptest! {
        #[test]
        fn read(
            input in ethernet_2_any(),
            dummy_data in proptest::collection::vec(any::<u8>(), 0..20)
        ) {
            // normal read
            let mut buffer = Vec::with_capacity(14 + dummy_data.len());
            input.write(&mut buffer).unwrap();
            buffer.extend(&dummy_data[..]);

            // calls with a valid result
            {
                let mut cursor = Cursor::new(&buffer);
                let result = Ethernet2Header::read(&mut cursor).unwrap();
                assert_eq!(input, result);
                assert_eq!(cursor.position(), 14);
            }

            // unexpected eof
            for len in 0..=13 {
                let mut cursor = Cursor::new(&buffer[0..len]);
                assert_eq!(
                    Ethernet2Header::read(&mut cursor)
                    .unwrap_err()
                    .kind(),
                    ErrorKind::UnexpectedEof
                );
            }
        }
    }

    proptest! {
        #[test]
        fn write_to_slice(input in ethernet_2_any()) {
            // normal write
            {
                let mut buffer: [u8;14] = [0;14];
                input.write_to_slice(&mut buffer).unwrap();
                assert_eq!(buffer, input.to_bytes());
            }
            // len to small
            for len in 0..14 {
                let mut buffer: [u8;14] = [0;14];
                assert_eq!(
                    SliceWriteSpaceError {
                        required_len: Ethernet2Header::LEN,
                        len,
                        layer: Layer::Ethernet2Header,
                        layer_start_offset: 0,
                    },
                    input.write_to_slice(&mut buffer[..len]).unwrap_err()
                );
            }
        }
    }

    proptest! {
        #[test]
        fn write(input in ethernet_2_any()) {
            // successful write
            {
                let mut buffer: Vec<u8> = Vec::with_capacity(14);
                input.write(&mut buffer).unwrap();
                assert_eq!(&buffer[..], &input.to_bytes());
            }

            // not enough memory for write (unexpected eof)
            for len in 0..8 {
                let mut buffer = [0u8;8];
                let mut writer = Cursor::new(&mut buffer[..len]);
                assert!(input.write(&mut writer).is_err());
            }
        }
    }

    proptest! {
        #[test]
        fn header_len(input in ethernet_2_any()) {
            assert_eq!(input.header_len(), 14);
        }
    }

    proptest! {
        #[test]
        fn to_bytes(input in ethernet_2_any()) {
            let ether_type_be = input.ether_type.0.to_be_bytes();
            assert_eq!(
                input.to_bytes(),
                [
                    input.destination[0],
                    input.destination[1],
                    input.destination[2],
                    input.destination[3],
                    input.destination[4],
                    input.destination[5],
                    input.source[0],
                    input.source[1],
                    input.source[2],
                    input.source[3],
                    input.source[4],
                    input.source[5],
                    ether_type_be[0],
                    ether_type_be[1],
                ]
            );
        }
    }

    proptest! {
        #[test]
        fn clone_eq(input in ethernet_2_any()) {
            assert_eq!(input, input.clone());
        }
    }

    proptest! {
        #[test]
        fn dbg(input in ethernet_2_any()) {
            assert_eq!(
                &format!(
                    "Ethernet2Header {{ source: {:?}, destination: {:?}, ether_type: {:?} }}",
                    input.source,
                    input.destination,
                    input.ether_type
                ),
                &format!("{:?}", input)
            );
        }
    }
}