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