| 1 | extern crate pnet_macros; |
| 2 | extern crate pnet_macros_support; |
| 3 | use pnet_macros::packet; |
| 4 | use pnet_macros_support::packet::PrimitiveValues; |
| 5 | |
| 6 | #[packet] |
| 7 | pub struct PacketWithVecConstruct { |
| 8 | banana: u8, |
| 9 | #[length_fn = "length_fn"] |
| 10 | #[construct_with(u64, u64)] |
| 11 | tomatoes: Vec<Identity>, |
| 12 | #[payload] |
| 13 | payload: Vec<u8>, |
| 14 | } |
| 15 | |
| 16 | fn length_fn(_: &PacketWithVecConstructPacket) -> usize { |
| 17 | 48 |
| 18 | } |
| 19 | |
| 20 | #[derive(PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug)] |
| 21 | pub struct Identity(pub(crate) [u8; Identity::LEN]); |
| 22 | |
| 23 | impl Identity { |
| 24 | const LEN: usize = 16; |
| 25 | |
| 26 | pub fn new(b0: u64, b1: u64) -> Identity { |
| 27 | let mut buf = [0u8; 16]; |
| 28 | buf[0..8].copy_from_slice(&b0.to_be_bytes()); |
| 29 | buf[8..16].copy_from_slice(&b1.to_be_bytes()); |
| 30 | Identity(buf) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl PrimitiveValues for Identity { |
| 35 | type T = (u64, u64); |
| 36 | fn to_primitive_values(&self) -> (u64, u64) { |
| 37 | ( |
| 38 | u64::from_be_bytes(self.0[0..8].try_into().unwrap()), |
| 39 | u64::from_be_bytes(self.0[8..16].try_into().unwrap()), |
| 40 | ) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn main() { |
| 45 | let test = PacketWithVecConstruct { |
| 46 | banana: 1, |
| 47 | tomatoes: vec![ |
| 48 | Identity([2u8; 16]), |
| 49 | Identity([3u8; 16]), |
| 50 | Identity([4u8; 16]) |
| 51 | ], |
| 52 | payload: vec![], |
| 53 | }; |
| 54 | |
| 55 | let mut buf = vec![0; PacketWithVecConstructPacket::packet_size(&test)]; |
| 56 | let mut packet = MutablePacketWithVecConstructPacket::new(&mut buf).unwrap(); |
| 57 | packet.populate(&test); |
| 58 | assert_eq!(packet.get_banana(), test.banana); |
| 59 | assert_eq!(packet.get_tomatoes(), test.tomatoes); |
| 60 | } |