etherparse
A library for parsing & writing a bunch of packet based protocols (EthernetII, IPv4, IPv6, UDP ...).
Currently supported are:
- Ethernet II
- IEEE 802.1Q VLAN Tagging Header
- IPv4
- IPv6 (missing extension headers, but supporting skipping them)
- UDP
Usage
First, add the following to your Cargo.toml
:
[]
= "0.3.0"
Next, add this to your crate root:
extern crate etherparse;
What is etherparse?
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:
- It is completly written in Rust and thoroughly tested.
- Special attention has been paid to avoid allocations or other syscalls whenever possible.
- The package is still in development and can & will still change.
- The current focus of development is on the most popular protocols in the internet & transport layer.
How to generate fake packet data?
Packet Builder
There is the option to use the PacketBuilder, which provides a high level interface to create UDP network packets. The PacketBuilder will take care of setting all the fields which can be deduced from the content and compositions of the packet (checksums, lengths, ethertype, ip protocol number).
let builder =
ethernet2 //destionation mac
.ipv4 //time to life
.udp; //desitnation port
//payload of the udp packet
let payload = ;
//serialize
//this will automatically set all length fields, checksums and identifiers (ethertype & protocol)
//before writing the packet out to "result"
builder.write.unwrap;
Check out the UdpPacketBuilder documentation for more informations.
Manually serialising each header
Alternativly 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.
Check out the documentations of the different elements for a more detailed documentation:
- Ethernet2Header.write
- SingleVlanHeader.write
- DoubleVlanHeader.write
- Ipv4Header.write
- Ipv4Header.write_raw
- Ipv6Header.write
- UdpHeader.write
Roadmap
- Generic packet parser (automaticly parsing of a packet based on its content)
- TCP
- IEEE 802.3
References
- Darpa Internet Program Protocol Specification RFC 791
- Internet Protocol, Version 6 (IPv6) Specification RFC 8200
- IANA Protocol Numbers
- Wikipedia IEEE_802.1Q
- User Datagram Protocol (UDP) RFC 768