1 use crate::*;
2 
3 /// Enum containing a slice of a supported ipv6 extension header.
4 ///
5 /// This enum is used as item type when iterating over a list of extension headers
6 /// with an [Ipv6ExtensionSliceIter].
7 ///
8 /// Note the following extension headers are missing from
9 /// this enum and currently not supported (list taken on 2021-07-17
10 /// from <https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml>):
11 ///
12 /// * Encapsulating Security Payload \[[RFC4303](https://datatracker.ietf.org/doc/html/rfc4303)\]
13 /// * Mobility Header \[[RFC6275](https://datatracker.ietf.org/doc/html/rfc6275)\]
14 /// * Host Identity Protocol \[[RFC7401](https://datatracker.ietf.org/doc/html/rfc7401)\]
15 /// * Shim6 Protocol \[[RFC5533](https://datatracker.ietf.org/doc/html/rfc5533)\]
16 /// * 253 Use for experimentation and testing \[[RFC3692](https://datatracker.ietf.org/doc/html/rfc3692)\]\[[RFC4727](https://datatracker.ietf.org/doc/html/rfc4727)\]
17 /// * 254 Use for experimentation and testing \[[RFC3692](https://datatracker.ietf.org/doc/html/rfc3692)\]\[[RFC4727](https://datatracker.ietf.org/doc/html/rfc4727)\]
18 #[derive(Clone, Debug, Eq, PartialEq)]
19 pub enum Ipv6ExtensionSlice<'a> {
20     /// IPv6 Hop-by-Hop Option \[[RFC8200](https://datatracker.ietf.org/doc/html/rfc8200)\]
21     HopByHop(Ipv6RawExtHeaderSlice<'a>),
22     /// Routing Header for IPv6 \[[RFC8200](https://datatracker.ietf.org/doc/html/rfc8200)\] \[[RFC5095](https://datatracker.ietf.org/doc/html/rfc5095)\]
23     Routing(Ipv6RawExtHeaderSlice<'a>),
24     /// Fragment Header for IPv6 \[[RFC8200](https://datatracker.ietf.org/doc/html/rfc8200)\]
25     Fragment(Ipv6FragmentHeaderSlice<'a>),
26     /// Destination Options for IPv6 \[[RFC8200](https://datatracker.ietf.org/doc/html/rfc8200)\]
27     DestinationOptions(Ipv6RawExtHeaderSlice<'a>),
28     /// Authentication Header \[[RFC4302](https://datatracker.ietf.org/doc/html/rfc4302)\]
29     Authentication(IpAuthHeaderSlice<'a>),
30 }
31 
32 #[cfg(test)]
33 mod test {
34     use super::*;
35     use crate::ip_number::*;
36 
37     #[test]
debug()38     fn debug() {
39         use alloc::{format, vec::Vec};
40         use Ipv6ExtensionSlice::*;
41         {
42             let header = Ipv6RawExtHeader::new_raw(UDP, &[1, 2, 3, 4, 5, 6]).unwrap();
43             let mut buffer = Vec::with_capacity(header.header_len());
44             header.write(&mut buffer).unwrap();
45             let slice = Ipv6RawExtHeaderSlice::from_slice(&buffer).unwrap();
46             assert_eq!(
47                 format!("HopByHop({:?})", slice),
48                 format!("{:?}", HopByHop(slice.clone()))
49             );
50             assert_eq!(
51                 format!("Routing({:?})", slice),
52                 format!("{:?}", Routing(slice.clone()))
53             );
54             assert_eq!(
55                 format!("DestinationOptions({:?})", slice),
56                 format!("{:?}", DestinationOptions(slice.clone()))
57             );
58         }
59         {
60             let header = Ipv6FragmentHeader::new(UDP, 1.try_into().unwrap(), true, 2);
61             let mut buffer = Vec::with_capacity(header.header_len());
62             header.write(&mut buffer).unwrap();
63             let slice = Ipv6FragmentHeaderSlice::from_slice(&buffer).unwrap();
64             assert_eq!(
65                 format!("Fragment({:?})", slice),
66                 format!("{:?}", Fragment(slice))
67             );
68         }
69         {
70             let header = IpAuthHeader::new(UDP, 1, 2, &[1, 2, 3, 4]).unwrap();
71             let mut buffer = Vec::with_capacity(header.header_len());
72             header.write(&mut buffer).unwrap();
73             let slice = IpAuthHeaderSlice::from_slice(&buffer).unwrap();
74             assert_eq!(
75                 format!("Authentication({:?})", slice),
76                 format!("{:?}", Authentication(slice.clone()))
77             );
78         }
79     }
80 
81     #[test]
clone_eq()82     fn clone_eq() {
83         use alloc::vec::Vec;
84         use Ipv6ExtensionSlice::*;
85 
86         let header = Ipv6RawExtHeader::new_raw(UDP, &[1, 2, 3, 4, 5, 6]).unwrap();
87         let mut buffer = Vec::with_capacity(header.header_len());
88         header.write(&mut buffer).unwrap();
89         let slice = Ipv6RawExtHeaderSlice::from_slice(&buffer).unwrap();
90 
91         let hop = HopByHop(slice.clone());
92         assert_eq!(hop.clone(), hop.clone());
93 
94         let route = Routing(slice.clone());
95         assert_eq!(route.clone(), route.clone());
96 
97         assert_ne!(route, hop);
98     }
99 }
100