pnet_layers
pnet_layers is a scapy like wrapper around the pnet crate to easily parse, craft or manipulate network packets.
For this pnet_layers defines two traits which can be implemented for structs that are deriving #[packet] defined by the pnet library. The first trait is the LayerImmutable implementing functions without cloning the underlining buffer. And the LayerMutable that allows to modify the packet or to craft new onces as shown below.
Crafting a packet
To craft, for example, a UDP packet including all layers down to Ethernet the EtherMut can be used.
// Create a new Ethernet Packet
let mut ether = new;
// Modify the packet. The `modify` function returns the pnet defined mutable packet to modify the different field.
if let Some = ether.modify
// Using the `add` function new layers can be added.
ether.add;
ether.add;
ether.add;
ether.add;
println!;
// Ether (s: 3c:ce:33:33:33:33, d: ff:ff:ff:ff:ff:ff:ff) > Vlan (id: 1) > Ipv4 (s: 0.0.0.0, d: 0.0.0.0) > Udp (s: 0, d: 0) > [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
// Using the .build() function all required params will be set including:
// - EtherType
// - Ipv4 size, checksum
// - UDP size, checksum
// Also some magic fields will be set. For example the IPv4 TTL value will be set to `MAGIC_IPV4_TTL`.
// This makes is possible to identify the packet later, for example, in a Wireshark trace.
// See all magic bytes in the `magics.rs` file
if let Some = ether.build
Parsing and manipulating
Packet can also be parsed from a u8 array and then modified.
/// Parsing the packet from a buffer in this case an Ethernet packet.
if let Some = from_buf
Creating a new layer
Currently only a few layers are defined in src/layers. If you want to add a new layer, please crate a new file in the layers folder with the protocol name. And implement the two traits. Most functions can be implemented by macros defined in macros.rs.
License
This project is licensed under the Apache-2.0 license