1#[cfg(test)]
4use crate::Packet;
5
6use alloc::vec::Vec;
7
8use xenet_macro::packet;
9use xenet_macro_helper::types::*;
10
11#[packet]
15pub struct Gre {
16 pub checksum_present: u1,
17 pub routing_present: u1,
18 pub key_present: u1,
19 pub sequence_present: u1,
20 pub strict_source_route: u1,
21 pub recursion_control: u3,
22 pub zero_flags: u5,
23 pub version: u3,
24 pub protocol_type: u16be, #[length_fn = "gre_checksum_length"]
26 pub checksum: Vec<U16BE>,
27 #[length_fn = "gre_offset_length"]
28 pub offset: Vec<U16BE>,
29 #[length_fn = "gre_key_length"]
30 pub key: Vec<U32BE>,
31 #[length_fn = "gre_sequence_length"]
32 pub sequence: Vec<U32BE>,
33 #[length_fn = "gre_routing_length"]
34 pub routing: Vec<u8>,
35 #[payload]
36 pub payload: Vec<u8>,
37}
38
39fn gre_checksum_length(gre: &GrePacket) -> usize {
40 (gre.get_checksum_present() | gre.get_routing_present()) as usize * 2
41}
42
43fn gre_offset_length(gre: &GrePacket) -> usize {
44 (gre.get_checksum_present() | gre.get_routing_present()) as usize * 2
45}
46
47fn gre_key_length(gre: &GrePacket) -> usize {
48 gre.get_key_present() as usize * 4
49}
50
51fn gre_sequence_length(gre: &GrePacket) -> usize {
52 gre.get_sequence_present() as usize * 4
53}
54
55fn gre_routing_length(gre: &GrePacket) -> usize {
56 if 0 == gre.get_routing_present() {
57 0
58 } else {
59 panic!("Source routed GRE packets not supported")
60 }
61}
62
63#[packet]
65pub struct U16BE {
66 number: u16be,
67 #[length = "0"]
68 #[payload]
69 unused: Vec<u8>,
70}
71
72#[packet]
74pub struct U32BE {
75 number: u32be,
76 #[length = "0"]
77 #[payload]
78 unused: Vec<u8>,
79}
80
81#[test]
82fn gre_packet_test() {
83 let mut packet = [0u8; 4];
84 {
85 let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
86 gre_packet.set_protocol_type(0x0800);
87 assert_eq!(gre_packet.payload().len(), 0);
88 }
89
90 let ref_packet = [
91 0x00, 0x00, 0x08, 0x00,
95 ];
96
97 assert_eq!(&ref_packet[..], &packet[..]);
98}
99
100#[test]
101fn gre_checksum_test() {
102 let mut packet = [0u8; 8];
103 {
104 let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
105 gre_packet.set_checksum_present(1);
106 assert_eq!(gre_packet.payload().len(), 0);
107 assert_eq!(gre_packet.get_checksum().len(), 1);
108 assert_eq!(gre_packet.get_offset().len(), 1);
109 }
110
111 let ref_packet = [
112 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 ];
119
120 assert_eq!(&ref_packet[..], &packet[..]);
121}