Expand description
pnet_layers
is a wrapper around the pnet
library offering
a more flexible way of manipulating packages like with scapy.
use pnet::util::MacAddr;
use pnet_layers::*;
use std::str::FromStr;
let mut ether = EtherMut::new();
if let Some(mut eth) = ether.modify() {
eth.set_source(MacAddr::from_str("3c:ce:33:33:33:33").unwrap());
eth.set_destination(MacAddr::broadcast());
}
ether.add(LayerMut::Vlan(VlanMut::new()));
ether.add(LayerMut::Ipv4(Ipv4Mut::new()));
ether.add(LayerMut::Udp(UdpMut::new()));
ether.add(LayerMut::Payload(PayloadMut::from_buf(vec![10; 10]).unwrap()));
for vlan_id in [1u16, 2, 3, 4] {
let mut ether = ether.clone();
if let Some(LayerMut::Vlan(vlan)) = ether.get_layer(&Layers::Vlan) {
vlan.modify().unwrap().set_vlan_identifier(vlan_id);
}
println!("{ether}");
// Ether (s: 3c:ce:33:33:33:33, d: 3c:ce:33:33:33:33) > 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]
// Ether (s: 3c:ce:33:33:33:33, d: 3c:ce:33:33:33:33) > Vlan (id: 2) > 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]
// Ether (s: 3c:ce:33:33:33:33, d: 3c:ce:33:33:33:33) > Vlan (id: 3) > 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]
// Ether (s: 3c:ce:33:33:33:33, d: 3c:ce:33:33:33:33) > Vlan (id: 4) > 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, like
// when doing an firewall test. See all magic bytes in the
// `magics.rs` file
if let Some(bytes) = ether.build() {
// bytes now be send
// let _ = tx.send_to(&bytes, None);
// EtherMut can also be created from a buffer...
if let Some(mut send) = EtherMut::from_buf(bytes) {
println!("{send}");
// Ether (s: 3c:ce:33:33:33:33, d: 3c:ce:33:33:33:33) > 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]
// ...
}
}
}
Modules§
- helper
- Some functions to easier craft specific packets
- magics
- Magic consists of different const values which
will be set if the value is not defined. These values allows to identify the packet when it
was send by
pnet_layers
. - traits
- optional traits
Macros§
- create_
add_ layer - create
add_layer
function - create_
default_ immutable - creates all default immutable functions
- create_
from_ buf - creates
from_buf
function - create_
get_ layer - create
get_layer
function - create_
modify - create
create_modify
function - create_
set_ payload - create
create_set_payload
function - create_
switch_ layer - creates
switch_src_dst
function - get_
layer - Shortcut for getting a layer
if let Some(vlan) = get_layer!(Vlan, ether) {}
- layers
- Implements basics for the different layers
Structs§
- Arp
- Immutable representation of an arp packet
- ArpMut
- Mutable representation of an arp packet
- Ether
- Immutable representation of an Ethernet packet
- Ether
Mut - Mutable representation of an Ethernet packet
- Icmp
- Immutable representation of an icmp packet
- IcmpMut
- Mutable representation of an icmp packet
- Ipv4
- Immutable representation of an Ipv4 packet
- Ipv6
- Immutable representation of an Ipv6 packet
- Ipv4Mut
- Mutable representation of an Ipv4 packet
- Ipv6Mut
- Mutable representation of an Ipv6 packet
- Payload
- Immutable representation of an arp packet
- Payload
Mut - Mutable representation of an arp packet
- Tcp
- Immutable representation of an UPD packet
- TcpMut
- Mutable representation of an UPD packet
- Udp
- Immutable representation of an UPD packet
- UdpMut
- Mutable representation of an UPD packet
- Vlan
- Immutable representation of an VLAN packet
- VlanMut
- Mutable representation of an VLAN packet
Enums§
- Layer
- Immutable layer based representation of a network package This can be used for zero-copy inspection of received packets
- Layer
Mut - Mutable layer based representation of a network package. This can be used for manipulating a packet or adding more layers
- Layers
- Supported layer types to make them identifiable
Traits§
- Layer
Immutable - Implements functions for immutable layer representation
- Layer
Mutable - Implements functions for packet manipulation