use crate::{types::*, Packet};
#[derive(Debug, Packet)]
pub struct Gre {
pub checksum_present: u1,
pub routing_present: u1,
pub key_present: u1,
pub sequence_present: u1,
pub strict_source_route: u1,
pub recursion_control: u3,
pub zero_flags: u5,
pub version: u3,
pub protocol_type: u16be, #[length = "(checksum_present | routing_present) * 2"]
pub checksum: Vec<U16BE>,
#[length = "(checksum_present | routing_present) * 2"]
pub offset: Vec<U16BE>,
#[length = "key_present * 4"]
pub key: Vec<U32BE>,
#[length = "sequence_present * 4"]
pub sequence: Vec<U32BE>,
#[length = "if routing_present == 0 { 0 } else { unimplemented!() }"]
pub routing: Vec<u8>,
#[payload]
pub payload: Vec<u8>,
}
#[derive(Debug, Packet)]
pub struct U16BE {
number: u16be,
#[length = "0"]
#[payload]
unused: Vec<u8>,
}
#[derive(Debug, Packet)]
pub struct U32BE {
number: u32be,
#[length = "0"]
#[payload]
unused: Vec<u8>,
}
#[test]
fn gre_packet_test() {
let mut packet = [0u8; 4];
{
let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
gre_packet.set_protocol_type(0x0800);
assert_eq!(gre_packet.payload().len(), 0);
}
let ref_packet = [
0x00,
0x00,
0x08,
0x00,
];
assert_eq!(&ref_packet[..], &packet[..]);
}
#[test]
fn gre_checksum_test() {
let mut packet = [0u8; 8];
{
let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
gre_packet.set_checksum_present(1);
assert_eq!(gre_packet.payload().len(), 0);
assert_eq!(gre_packet.get_checksum().len(), 1);
assert_eq!(gre_packet.get_offset().len(), 1);
}
let ref_packet = [
0x80,
0x00,
0x00,
0x00, 0x00,
0x00, 0x00,
0x00,
];
assert_eq!(&ref_packet[..], &packet[..]);
}