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 Some(Arp(value)) => println!(" Arp {:?}", value),
96 None => {}
97 }
98
99 match value.transport {
100 Some(Icmpv4(value)) => println!(" Icmpv4 {:?}", value),
101 Some(Icmpv6(value)) => println!(" Icmpv6 {:?}", value),
102 Some(Udp(value)) => println!(
103 " UDP {:?} -> {:?}",
104 value.source_port(),
105 value.destination_port()
106 ),
107 Some(Tcp(value)) => {
108 println!(
109 " TCP {:?} -> {:?}",
110 value.source_port(),
111 value.destination_port()
112 );
113 let options: Vec<Result<TcpOptionElement, TcpOptionReadError>> =
114 value.options_iterator().collect();
115 println!(" {:?}", options);
116 }
117 None => {}
118 }
119 }
120 }
121 }
122