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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::{err::*, *};

/// Slice containing the UDP headers & payload.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UdpSlice<'a> {
    slice: &'a [u8],
}

impl<'a> UdpSlice<'a> {
    /// Decode length from UDP header and restrict slice to the length
    /// of the header including the payload.
    ///
    /// Note that this method fall backs to the length of the slice
    /// in the case the length field in the UDP header is set to zero.
    pub fn from_slice(slice: &'a [u8]) -> Result<UdpSlice<'a>, LenError> {
        // slice header
        let header = UdpHeaderSlice::from_slice(slice)?;

        // validate the length of the slice
        let len: usize = header.length().into();
        if slice.len() < len {
            return Err(LenError {
                required_len: len,
                len: slice.len(),
                len_source: LenSource::Slice,
                layer: Layer::UdpPayload,
                layer_start_offset: 0,
            });
        }

        // fallback to the slice length in case length is set to 0
        if len == 0 {
            Ok(UdpSlice { slice })
        } else {
            // validate the length
            if len < UdpHeader::LEN {
                // TODO: Should this replaced with a custom error?
                Err(LenError {
                    required_len: UdpHeader::LEN,
                    len,
                    len_source: LenSource::UdpHeaderLen,
                    layer: Layer::UdpHeader,
                    layer_start_offset: 0,
                })
            } else {
                Ok(UdpSlice {
                    // SAFETY: Safe as slice.len() was validated before to
                    // be at least as big as "len".
                    slice: unsafe { core::slice::from_raw_parts(slice.as_ptr(), len) },
                })
            }
        }
    }

    /// Try decoding length from UDP header and restrict slice to the length
    /// of the header including the payload if possible. If not the slice length
    /// is used as a fallback value.
    ///
    /// Note that this method fall also backs to the length of the slice
    /// in the case the length field in the UDP header is set to zero or smaller
    /// then the minimum header length.
    pub fn from_slice_lax(slice: &'a [u8]) -> Result<UdpSlice<'a>, LenError> {
        // slice header
        let header = UdpHeaderSlice::from_slice(slice)?;

        // validate the length of the slice and fallback to the slice
        // length if the slice is smaller then expected or zero.
        let len: usize = header.length().into();
        if slice.len() < len || len < UdpHeader::LEN {
            Ok(UdpSlice { slice })
        } else {
            Ok(UdpSlice {
                // SAFETY: Safe as slice.len() was validated before to
                // be at least as big as "len".
                slice: unsafe { core::slice::from_raw_parts(slice.as_ptr(), len) },
            })
        }
    }

    /// Return the slice containing the UDP header & payload.
    #[inline]
    pub fn slice(&self) -> &'a [u8] {
        self.slice
    }

    /// Return the slice containing the UDP header.
    #[inline]
    pub fn header_slice(&self) -> &'a [u8] {
        unsafe {
            // SAFETY: Safe as the slice length was verified
            // to be at least UdpHeader::LEN by "from_slice".
            core::slice::from_raw_parts(self.slice.as_ptr(), UdpHeader::LEN)
        }
    }

    /// Returns the slice containing the UDP payload.
    #[inline]
    pub fn payload(&self) -> &'a [u8] {
        unsafe {
            // SAFETY: Safe as the slice length was verified
            // to be at least UdpHeader::LEN by "from_slice".
            core::slice::from_raw_parts(
                self.slice.as_ptr().add(UdpHeader::LEN),
                self.slice.len() - UdpHeader::LEN,
            )
        }
    }

    /// Value that was used to determine the length of the payload.
    #[inline]
    pub fn payload_len_source(&self) -> LenSource {
        if usize::from(self.length()) == self.slice.len() {
            LenSource::UdpHeaderLen
        } else {
            LenSource::Slice
        }
    }

    /// Reads the "udp source port" in the UDP header.
    #[inline]
    pub fn source_port(&self) -> u16 {
        // SAFETY:
        // Safe as the contructor checks that the slice has
        // at least the length of UdpHeader::LEN (8).
        unsafe { get_unchecked_be_u16(self.slice.as_ptr()) }
    }

    /// Reads the "udp destination port" in the UDP header.
    #[inline]
    pub fn destination_port(&self) -> u16 {
        // SAFETY:
        // Safe as the contructor checks that the slice has
        // at least the length of UdpHeader::LEN (8).
        unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(2)) }
    }

    /// Reads the "length" field in the UDP header.
    #[inline]
    pub fn length(&self) -> u16 {
        // SAFETY:
        // Safe as the contructor checks that the slice has
        // at least the length of UdpHeader::LEN (8).
        unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(4)) }
    }

    /// Reads the "checksum" from the slice.
    #[inline]
    pub fn checksum(&self) -> u16 {
        // SAFETY:
        // Safe as the contructor checks that the slice has
        // at least the length of UdpHeader::LEN (8).
        unsafe { get_unchecked_be_u16(self.slice.as_ptr().add(6)) }
    }

    /// Length of the UDP header (equal to [`crate::UdpHeader::LEN`]).
    #[inline]
    pub const fn header_len(&self) -> usize {
        UdpHeader::LEN
    }

    /// Length of the UDP header in an [`u16`] (equal to [`crate::UdpHeader::LEN_U16`]).
    #[inline]
    pub const fn header_len_u16(&self) -> u16 {
        UdpHeader::LEN_U16
    }

    /// Decode all the fields of the UDP header and copy the results
    /// to a UdpHeader struct.
    #[inline]
    pub fn to_header(&self) -> UdpHeader {
        UdpHeader {
            source_port: self.source_port(),
            destination_port: self.destination_port(),
            length: self.length(),
            checksum: self.checksum(),
        }
    }
}

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

    proptest! {
        #[test]
        fn debug_clone_eq(
            udp_base in udp_any()
        ) {
            let payload: [u8;4] = [1,2,3,4];
            let mut data = Vec::with_capacity(
                udp_base.header_len() +
                payload.len()
            );
            let mut udp = udp_base.clone();
            udp.length = (UdpHeader::LEN + payload.len()) as u16;
            data.extend_from_slice(&udp.to_bytes());
            data.extend_from_slice(&payload);

            // decode packet
            let slice = UdpSlice::from_slice(&data).unwrap();

            // check debug output
            prop_assert_eq!(
                format!("{:?}", slice),
                format!(
                    "UdpSlice {{ slice: {:?} }}",
                    &data[..]
                )
            );
            prop_assert_eq!(slice.clone(), slice);
        }
    }

    proptest! {
        #[test]
        fn getters(
            udp_base in udp_any()
        ) {
            let udp = {
                let mut udp = udp_base.clone();
                udp.length = UdpHeader::LEN as u16;
                udp
            };
            let data = {
                let mut data = Vec::with_capacity(
                    udp.header_len()
                );
                data.extend_from_slice(&udp.to_bytes());
                data
            };

            // normal decode
            {
                let slice = UdpSlice::from_slice(&data).unwrap();
                assert_eq!(slice.slice(), &data);
                assert_eq!(slice.header_slice(), &data);
                assert_eq!(slice.payload(), &[]);
                assert_eq!(slice.source_port(), udp.source_port);
                assert_eq!(slice.destination_port(), udp.destination_port);
                assert_eq!(slice.length(), udp.length);
                assert_eq!(slice.checksum(), udp.checksum);
                assert_eq!(slice.to_header(), udp);
            }
        }
    }

    proptest! {
        #[test]
        fn from_slice(
            udp_base in udp_any()
        ) {
            let payload: [u8;4] = [1,2,3,4];
            let udp = {
                let mut udp = udp_base.clone();
                udp.length = (UdpHeader::LEN + payload.len()) as u16;
                udp
            };
            let data = {
                let mut data = Vec::with_capacity(
                    udp.header_len() +
                    payload.len()
                );
                data.extend_from_slice(&udp.to_bytes());
                data.extend_from_slice(&payload);
                data
            };

            // normal decode
            {
                let slice = UdpSlice::from_slice(&data).unwrap();
                assert_eq!(udp, slice.to_header());
                assert_eq!(payload, slice.payload());
            }

            // decode a payload smaller then the given slice
            {
                let mut mod_data = data.clone();
                let reduced_len = (UdpHeader::LEN + payload.len() - 1) as u16;
                // inject the reduced length
                {
                    let rl_be = reduced_len.to_be_bytes();
                    mod_data[4] = rl_be[0];
                    mod_data[5] = rl_be[1];
                }

                let slice = UdpSlice::from_slice(&mod_data).unwrap();
                assert_eq!(
                    slice.to_header(),
                    {
                        let mut expected = slice.to_header();
                        expected.length = reduced_len;
                        expected
                    }
                );
                assert_eq!(&payload[..payload.len() - 1], slice.payload());
            }

            // if length is zero the length given by the slice should be used
            {
                // inject zero as length
                let mut mod_data = data.clone();
                mod_data[4] = 0;
                mod_data[5] = 0;

                let slice = UdpSlice::from_slice(&mod_data).unwrap();

                assert_eq!(slice.source_port(), udp_base.source_port);
                assert_eq!(slice.destination_port(), udp_base.destination_port);
                assert_eq!(slice.checksum(), udp_base.checksum);
                assert_eq!(slice.length(), 0);
                assert_eq!(&payload, slice.payload());
            }

            // too little data to even decode the header
            for len in 0..UdpHeader::LEN {
                assert_eq!(
                    UdpSlice::from_slice(&data[..len]).unwrap_err(),
                    LenError {
                        required_len: UdpHeader::LEN,
                        len,
                        len_source: LenSource::Slice,
                        layer: Layer::UdpHeader,
                        layer_start_offset: 0,
                    }
                );
            }

            // slice length smaller then the length described in the header
            assert_eq!(
                UdpSlice::from_slice(&data[..data.len() - 1]).unwrap_err(),
                LenError {
                    required_len: data.len(),
                    len: data.len() - 1,
                    len_source: LenSource::Slice,
                    layer: Layer::UdpPayload,
                    layer_start_offset: 0,
                }
            );

            // length in header smaller than the header itself
            {
                let mut mod_data = data.clone();
                // inject the reduced length
                {
                    let len_be = ((UdpHeader::LEN - 1) as u16).to_be_bytes();
                    mod_data[4] = len_be[0];
                    mod_data[5] = len_be[1];
                }
                assert_eq!(
                    UdpSlice::from_slice(&mod_data).unwrap_err(),
                    LenError {
                        required_len: UdpHeader::LEN,
                        len: UdpHeader::LEN - 1,
                        len_source: LenSource::UdpHeaderLen,
                        layer: Layer::UdpHeader,
                        layer_start_offset: 0
                    }
                );
            }
        }
    }

    proptest! {
        #[test]
        fn from_slice_lax(
            udp_base in udp_any()
        ) {
            let payload: [u8;4] = [1,2,3,4];
            let udp = {
                let mut udp = udp_base.clone();
                udp.length = (UdpHeader::LEN + payload.len()) as u16;
                udp
            };
            let data = {
                let mut data = Vec::with_capacity(
                    udp.header_len() +
                    payload.len()
                );
                data.extend_from_slice(&udp.to_bytes());
                data.extend_from_slice(&payload);
                data
            };

            // normal decode
            {
                let slice = UdpSlice::from_slice_lax(&data).unwrap();
                assert_eq!(udp, slice.to_header());
                assert_eq!(payload, slice.payload());
                assert_eq!(slice.payload_len_source(), LenSource::UdpHeaderLen);
            }

            // decode a payload smaller then the given slice
            {
                let mut mod_data = data.clone();
                let reduced_len = (UdpHeader::LEN + payload.len() - 1) as u16;
                // inject the reduced length
                {
                    let rl_be = reduced_len.to_be_bytes();
                    mod_data[4] = rl_be[0];
                    mod_data[5] = rl_be[1];
                }

                let slice = UdpSlice::from_slice_lax(&mod_data).unwrap();
                assert_eq!(
                    slice.to_header(),
                    {
                        let mut expected = slice.to_header();
                        expected.length = reduced_len;
                        expected
                    }
                );
                assert_eq!(&payload[..payload.len() - 1], slice.payload());
                assert_eq!(slice.payload_len_source(), LenSource::UdpHeaderLen);
            }

            // if length is zero the length given by the slice should be used
            for len in 0..UdpHeader::LEN_U16{
                // inject zero as length
                let mut mod_data = data.clone();
                mod_data[4] = len.to_be_bytes()[0];
                mod_data[5] = len.to_be_bytes()[1];

                let slice = UdpSlice::from_slice_lax(&mod_data).unwrap();

                assert_eq!(slice.source_port(), udp_base.source_port);
                assert_eq!(slice.destination_port(), udp_base.destination_port);
                assert_eq!(slice.checksum(), udp_base.checksum);
                assert_eq!(slice.length(), len);
                assert_eq!(&payload, slice.payload());
                assert_eq!(slice.payload_len_source(), LenSource::Slice);
            }

            // too little data to even decode the header
            for len in 0..UdpHeader::LEN {
                assert_eq!(
                    UdpSlice::from_slice_lax(&data[..len]).unwrap_err(),
                    LenError {
                        required_len: UdpHeader::LEN,
                        len,
                        len_source: LenSource::Slice,
                        layer: Layer::UdpHeader,
                        layer_start_offset: 0,
                    }
                );
            }

            // slice length smaller then the length described in the header
            {
                let slice = UdpSlice::from_slice_lax(&data[..data.len() - 1]).unwrap();
                assert_eq!(udp, slice.to_header());
                assert_eq!(&payload[..payload.len() - 1], slice.payload());
                assert_eq!(slice.payload_len_source(), LenSource::Slice);
            }
        }
    }
}