zero-packet
Super simple library to efficiently build and parse network packets in-place with zero overhead.
No async, no allocations, no dependencies, no macros, no std, no unsafe. It simply cannot be easier.
Use zero-packet
if you are working with raw sockets.
Supported protocols:
- Ethernet
- ARP
- IPv4
- TCP
- UDP
- ICMP
Usage
Getting started
Install via your command line:
Or add the following to your Cargo.toml
:
[]
= "0.0.1"
PacketBuilder
If you want to create network packets manually and efficiently, look no further.
// Raw packet that we will mutate in-place.
// Ethernet header (14 bytes) + IPv4 header (20 bytes) + UDP header (8 bytes) = 42 bytes.
let mut packet =
// Some random payload (11 bytes).
let payload = b"Hello, UDP!";
// PacketBuilder which holds a mutable reference to the byte slice.
let mut packet_builder = new;
// Sets Ethernet, IPv4 and UDP header fields.
// Optional: add payload to the packet.
// Encapsulates everything in the given byte slice.
packet_builder
.ethernet?
.ipv4?
.udp?
.payload?;
PacketParser
Parsing any received byte slice for which we don't know ahead of time what type of packet it is.
// Some byte slice that we have received.
// We don't know yet what it contains.
let packet = ;
// PacketParser is a zero-copy packet parser.
// The `parse` method determines on its own what protocol headers are present.
// The method is strict about what input it accepts and validates certain fields.
let parsed = parse?;
// Now it is as easy as this.
if let Some = parsed.ethernet
// Or this.
if let Some = parsed.ipv4
// Alternatively, just parse headers directly manually.
// By adjusting the index of the slice you can find different headers.
if let Some = new?
Roadmap
Upcoming features:
- IPv6
- ICMPv6
- IPsec
- ...