• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::*;
2 
3 /// Laxly identified payload of an IP packet (potentially incomplete).
4 ///
5 /// To check if the payload is complete check the `incomplete` field.
6 #[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
7 pub struct LaxIpPayloadSlice<'a> {
8     /// True if the length field in the IP header indicates more data
9     /// should be present but it was not (aka the packet data is cut off).
10     ///
11     /// Note that this different from fragmentation. If a packet is
12     /// fragmented the length field in the individual IP headers is
13     /// still correctly set.
14     pub incomplete: bool,
15 
16     /// Identifying content of the payload.
17     pub ip_number: IpNumber,
18 
19     /// True if the payload is not complete and has been fragmented.
20     ///
21     /// This can occur if the IPv4 incdicates that the payload
22     /// has been fragmented or if there is an IPv6 fragmentation
23     /// header indicating that the payload has been fragmented.
24     pub fragmented: bool,
25 
26     /// Length field that was used to determine the length
27     /// of the payload (e.g. IPv6 "payload_length" field).
28     pub len_source: LenSource,
29 
30     /// Payload
31     pub payload: &'a [u8],
32 }
33 
34 #[cfg(test)]
35 mod test {
36     use super::*;
37     use alloc::format;
38 
39     #[test]
debug()40     fn debug() {
41         let s = LaxIpPayloadSlice {
42             incomplete: false,
43             ip_number: IpNumber::UDP,
44             fragmented: true,
45             len_source: LenSource::Slice,
46             payload: &[],
47         };
48         assert_eq!(
49             format!(
50                 "LaxIpPayloadSlice {{ incomplete: {:?}, ip_number: {:?}, fragmented: {:?}, len_source: {:?}, payload: {:?} }}",
51                 s.incomplete,
52                 s.ip_number,
53                 s.fragmented,
54                 s.len_source,
55                 s.payload
56             ),
57             format!("{:?}", s)
58         );
59     }
60 
61     #[test]
clone_eq_hash_ord()62     fn clone_eq_hash_ord() {
63         let s = LaxIpPayloadSlice {
64             incomplete: false,
65             ip_number: IpNumber::UDP,
66             fragmented: true,
67             len_source: LenSource::Slice,
68             payload: &[],
69         };
70         assert_eq!(s.clone(), s);
71 
72         use std::collections::hash_map::DefaultHasher;
73         use std::hash::{Hash, Hasher};
74 
75         let a_hash = {
76             let mut hasher = DefaultHasher::new();
77             s.hash(&mut hasher);
78             hasher.finish()
79         };
80         let b_hash = {
81             let mut hasher = DefaultHasher::new();
82             s.clone().hash(&mut hasher);
83             hasher.finish()
84         };
85         assert_eq!(a_hash, b_hash);
86 
87         use std::cmp::Ordering;
88         assert_eq!(s.clone().cmp(&s), Ordering::Equal);
89         assert_eq!(s.clone().partial_cmp(&s), Some(Ordering::Equal));
90     }
91 }
92