Crates.io | etherparse |
lib.rs | etherparse |
version | 0.16.0 |
source | src |
created_at | 2018-02-19 21:56:13.787843 |
updated_at | 2024-09-17 07:50:27.623498 |
description | A library for parsing & writing a bunch of packet based protocols (EthernetII, IPv4, IPv6, UDP, TCP ...). |
homepage | |
repository | https://github.com/JulianSchmid/etherparse |
max_upload_size | |
id | 51967 |
size | 2,651,750 |
A zero allocation supporting library for parsing & writing a bunch of packet based protocols (EthernetII, IPv4, IPv6, UDP, TCP ...).
Currently supported are:
Reconstruction of fragmented IP packets is also supported, but requires allocations.
Add the following to your Cargo.toml
:
[dependencies]
etherparse = "0.16"
Etherparse is intended to provide the basic network parsing functions that allow for easy analysis, transformation or generation of recorded network data.
Some key points are:
Etherparse gives you two options for parsing network packages automatically:
Here the different components in a packet are separated without parsing all their fields. For each header a slice is generated that allows access to the fields of a header.
match SlicedPacket::from_ethernet(&packet) {
Err(value) => println!("Err {:?}", value),
Ok(value) => {
println!("link: {:?}", value.link);
println!("vlan: {:?}", value.vlan);
println!("net: {:?}", value.net); // contains ip
println!("transport: {:?}", value.transport);
}
}
This is the faster option if your code is not interested in all fields of all the headers. It is a good choice if you just want filter or find packets based on a subset of the headers and/or their fields.
Depending from which point downward you want to slice a package check out the functions:
SlicedPacket::from_ethernet
for parsing from an Ethernet II header downwardsSlicedPacket::from_linux_sll
for parsing from a Linux Cooked Capture v1 (SLL) downwardsSlicedPacket::from_ether_type
for parsing a slice starting after an Ethernet II headerSlicedPacket::from_ip
for parsing from an IPv4 or IPv6 downwardsIn case you want to parse cut off packets (e.g. packets returned in in ICMP message) you can use the "lax" parsing methods:
LaxSlicedPacket::from_ethernet
for parsing from an Ethernet II header downwardsLaxSlicedPacket::from_ether_type
for parsing a slice starting after an Ethernet II headerLaxSlicedPacket::from_ip
for parsing from an IPv4 or IPv6 downwardsThis option deserializes all known headers and transfers their contents to header structs.
match PacketHeaders::from_ethernet_slice(&packet) {
Err(value) => println!("Err {:?}", value),
Ok(value) => {
println!("link: {:?}", value.link);
println!("vlan: {:?}", value.vlan);
println!("net: {:?}", value.net); // contains ip
println!("transport: {:?}", value.transport);
}
}
This option is slower then slicing when only few fields are accessed. But it can be the faster option or useful if you are interested in most fields anyways or if you want to re-serialize the headers with modified values.
Depending from which point downward you want to unpack a package check out the functions
PacketHeaders::from_ethernet_slice
for parsing from an Ethernet II header downwardsPacketHeaders::from_ether_type
for parsing a slice starting after an Ethernet II headerPacketHeaders::from_ip_slice
for parsing from an IPv4 or IPv6 downwardsIn case you want to parse cut off packets (e.g. packets returned in in ICMP message) you can use the "lax" parsing methods:
LaxPacketHeaders::from_ethernet
for parsing from an Ethernet II header downwardsLaxPacketHeaders::from_ether_type
for parsing a slice starting after an Ethernet II headerLaxPacketHeaders::from_ip
for parsing from an IPv4 or IPv6 downwardsIt is also possible to only slice one packet layer:
Ethernet2Slice::from_slice_without_fcs
& Ethernet2Slice::from_slice_with_crc32_fcs
LinuxSllSlice::from_slice
SingleVlanSlice::from_slice
& DoubleVlanSlice::from_slice
IpSlice::from_slice
& LaxIpSlice::from_slice
Ipv4Slice::from_slice
& LaxIpv4Slice::from_slice
Ipv6Slice::from_slice
& LaxIpv6Slice::from_slice
UdpSlice::from_slice
& UdpSlice::from_slice_lax
TcpSlice::from_slice
Icmpv4Slice::from_slice
Icmpv6Slice::from_slice
The resulting data types allow access to both the header(s) and the payload of the layer and will automatically limit the length of payload if the layer has a length field limiting the payload (e.g. the payload of IPv6 packets will be limited by the "payload length" field in an IPv6 header).
It is also possible just to parse headers. Have a look at the documentation for the following [NAME]HeaderSlice.from_slice methods, if you want to just slice the header:
Ethernet2HeaderSlice::from_slice
LinuxSllHeaderSlice::from_slice
SingleVlanHeaderSlice::from_slice
DoubleVlanHeaderSlice::from_slice
Ipv4HeaderSlice::from_slice
Ipv4ExtensionsSlice::from_slice
Ipv6HeaderSlice::from_slice
Ipv6ExtensionsSlice::from_slice
Ipv6RawExtHeaderSlice::from_slice
IpAuthHeaderSlice::from_slice
Ipv6FragmentHeaderSlice::from_slice
UdpHeaderSlice::from_slice
TcpHeaderSlice::from_slice
And for deserialization into the corresponding header structs have a look at:
Ethernet2Header::read
& Ethernet2Header::from_slice
LinuxSllHeader::read
& LinuxSllHeader::from_slice
SingleVlanHeader::read
& SingleVlanHeader::from_slice
DoubleVlanHeader::read
& DoubleVlanHeader::from_slice
IpHeaders::read
& IpHeaders::from_slice
Ipv4Header::read
& Ipv4Header::from_slice
Ipv4Extensions::read
& Ipv4Extensions::from_slice
Ipv6Header::read
& Ipv6Header::from_slice
Ipv6Extensions::read
& Ipv6Extensions::from_slice
Ipv6RawExtHeader::read
& Ipv6RawExtHeader::from_slice
IpAuthHeader::read
& IpAuthHeader::from_slice
Ipv6FragmentHeader::read
& Ipv6FragmentHeader::from_slice
UdpHeader::read
& UdpHeader::from_slice
TcpHeader::read
& TcpHeader::from_slice
Icmpv4Header::read
& Icmpv4Header::from_slice
Icmpv6Header::read
& Icmpv6Header::from_slice
The PacketBuilder struct provides a high level interface for quickly creating network packets. The PacketBuilder will automatically set fields which can be deduced from the content and compositions of the packet itself (e.g. checksums, lengths, ethertype, ip protocol number).
use etherparse::PacketBuilder;
let builder = PacketBuilder::
ethernet2([1,2,3,4,5,6], //source mac
[7,8,9,10,11,12]) //destination mac
.ipv4([192,168,1,1], //source ip
[192,168,1,2], //destination ip
20) //time to life
.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 result
let mut result = Vec::<u8>::with_capacity(builder.size(payload.len()));
//serialize
//this will automatically set all length fields, checksums and identifiers (ethertype & protocol)
//before writing the packet out to "result"
builder.write(&mut result, &payload).unwrap();
There is also an example for TCP packets available.
Check out the PacketBuilder documentation for more information.
Alternatively it is possible to manually build a packet (example). Generally each struct representing a header has a "write" method that allows it to be serialized. These write methods sometimes automatically calculate checksums and fill them in. In case this is unwanted behavior (e.g. if you want to generate a packet with an invalid checksum), it is also possible to call a "write_raw" method that will simply serialize the data without doing checksum calculations.
Read the documentations of the different methods for a more details:
Ethernet2Header::to_bytes
& Ethernet2Header::write
LinuxSllHeader::to_bytes
& LinuxSllHeader::write
SingleVlanHeader::to_bytes
& SingleVlanHeader::write
DoubleVlanHeader::to_bytes
& DoubleVlanHeader::write
Ipv4Header::to_bytes
& Ipv4Header::write
& Ipv4Header::write_raw
Ipv4Extensions::write
Ipv6Header::to_bytes
& Ipv6Header::write
Ipv6Extensions::write
Ipv6RawExtHeader::to_bytes
& Ipv6RawExtHeader::write
IpAuthHeader::to_bytes
& IpAuthHeader::write
Ipv6FragmentHeader::to_bytes
& Ipv6FragmentHeader::write
UdpHeader::to_bytes
& UdpHeader::write
TcpHeader::to_bytes
& TcpHeader::write
Icmpv4Header::to_bytes
& Icmpv4Header::write
Icmpv6Header::to_bytes
& Icmpv6Header::write
Licensed under either of Apache License, Version 2.0 or MIT license at your option. The corresponding license texts can be found in the LICENSE-APACHE file and the LICENSE-MIT file.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be licensed as above, without any additional terms or conditions.