1 use etherparse::*;
2 
main()3 fn main() {
4     //setup some network data to parse
5     let builder = PacketBuilder::ethernet2(
6         //source mac
7         [1, 2, 3, 4, 5, 6],
8         //destination mac
9         [7, 8, 9, 10, 11, 12],
10     )
11     .ipv4(
12         //source ip
13         [192, 168, 1, 1],
14         //destination ip
15         [192, 168, 1, 2],
16         //time to life
17         20,
18     )
19     .udp(
20         21,   //source port
21         1234, //desitnation port
22     );
23 
24     //payload of the udp packet
25     let payload = [1, 2, 3, 4, 5, 6, 7, 8];
26 
27     //get some memory to store the serialized data
28     let mut serialized = Vec::<u8>::with_capacity(builder.size(payload.len()));
29     builder.write(&mut serialized, &payload).unwrap();
30 
31     //slice the packet into the different header components
32     let sliced_packet = SlicedPacket::from_ethernet(&serialized);
33 
34     //print some informations about the sliced packet
35     match sliced_packet {
36         Err(value) => println!("Err {:?}", value),
37         Ok(value) => {
38             println!("Ok");
39             use etherparse::{LinkSlice::*, NetSlice::*, TransportSlice::*, VlanSlice::*};
40 
41             match value.link {
42                 Some(Ethernet2(value)) => println!(
43                     "  Ethernet2 {:?} => {:?}",
44                     value.source(),
45                     value.destination()
46                 ),
47                 Some(LinuxSll(value)) => println!(
48                     "  LinuxSll (packet type: {:?}, source address: {:?})",
49                     value.packet_type(),
50                     value.sender_address(),
51                 ),
52                 Some(EtherPayload(payload)) => {
53                     println!("  EtherPayload (ether type {:?})", payload.ether_type)
54                 }
55                 Some(LinuxSllPayload(payload)) => {
56                     println!(
57                         "  LinuxSllPayload (protocol type {:?})",
58                         payload.protocol_type
59                     )
60                 }
61                 None => {}
62             }
63 
64             match value.vlan {
65                 Some(SingleVlan(value)) => println!("  SingleVlan {:?}", value.vlan_identifier()),
66                 Some(DoubleVlan(value)) => println!(
67                     "  DoubleVlan {:?}, {:?}",
68                     value.outer().vlan_identifier(),
69                     value.inner().vlan_identifier()
70                 ),
71                 None => {}
72             }
73 
74             match value.net {
75                 Some(Ipv4(ipv4)) => {
76                     println!(
77                         "  Ipv4 {:?} => {:?}",
78                         ipv4.header().source_addr(),
79                         ipv4.header().destination_addr()
80                     );
81                     if false == ipv4.extensions().is_empty() {
82                         println!("    {:?}", ipv4.extensions());
83                     }
84                 }
85                 Some(Ipv6(ipv6)) => {
86                     println!(
87                         "  Ipv6 {:?} => {:?}",
88                         ipv6.header().source_addr(),
89                         ipv6.header().destination_addr()
90                     );
91                     if false == ipv6.extensions().is_empty() {
92                         println!("    {:?}", ipv6.extensions());
93                     }
94                 }
95                 None => {}
96             }
97 
98             match value.transport {
99                 Some(Icmpv4(value)) => println!(" Icmpv4 {:?}", value),
100                 Some(Icmpv6(value)) => println!(" Icmpv6 {:?}", value),
101                 Some(Udp(value)) => println!(
102                     "  UDP {:?} -> {:?}",
103                     value.source_port(),
104                     value.destination_port()
105                 ),
106                 Some(Tcp(value)) => {
107                     println!(
108                         "  TCP {:?} -> {:?}",
109                         value.source_port(),
110                         value.destination_port()
111                     );
112                     let options: Vec<Result<TcpOptionElement, TcpOptionReadError>> =
113                         value.options_iterator().collect();
114                     println!("    {:?}", options);
115                 }
116                 None => {}
117             }
118         }
119     }
120 }
121