Debian Codegraph @

tests/run-pass/packet_in_packet.rs

raw symbols SCIP
1 // Copyright (c) 2015 Robert Clipsham <[email protected]>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 extern crate pnet_macros;
10 extern crate pnet_macros_support;
11 use pnet_macros::packet;
12 #[packet]
13 pub struct PacketWithPayload {
14 banana: u8,
15 length: u8,
16 header_length: u8,
17 #[length_fn = "length_fn"]
18 packet_option: Vec<PacketOption>,
19 #[payload]
20 payload: Vec<u8>,
21 }
22
23 #[packet]
24 pub struct PacketOption {
25 pineapple: u8,
26 length: u8,
27 #[length_fn = "option_length_fn"]
28 #[payload]
29 payload: Vec<u8>,
30 }
31
32 fn length_fn(packet_with_payload: &PacketWithPayloadPacket) -> usize {
33 packet_with_payload.get_header_length() as usize - 2
34 }
35
36 fn option_length_fn(packet_option: &PacketOptionPacket) -> usize {
37 packet_option.get_length() as usize - 2
38 }
39
40 fn main() {
41 let data = [1, 8, 5, 6, 3, 1, 9, 10];
42 let packet = PacketWithPayloadPacket::new(&data[..]).unwrap();
43
44 let packet_option = packet.get_packet_option();
45 assert_eq!(packet_option.first().unwrap().pineapple, 6);
46 assert_eq!(packet_option.first().unwrap().length, 3);
47 }