packet_rs is a Rust based Scapy alternative
packetrs is a rust based alternative to the popular python Scapy packet library. It tries to provide a scapy like API interface to define new headers and construct packets.
packetrs has the most common networking headers already pre-defined.
The core of the library is the make_header macro which provides a flexible way to create new headers.
Describe a new header in the below format with defaults as a vector.
make_header! {
<header_name> <length in bytes> (
<field_name1> <start_bit> - <end_bit>,
<field_name2> <start_bit> - <end_bit>,
)
<optional default data vec>
};
Add a new header by using the make_header macro. This automatically creates a set_field() and a field() helper methods for each field.
```rust
extern crate packet_rs;
use packetrs::headers::*; use packetrs::Packet;
makeheader!( MyHeader 4 ( field1: 0-2, field2: 3-3, field3: 4-15, field_4: 16-31 ) vec![0x0, 0xa, 0x8, 0x0] // <= optional default data ); ```
```rust // Call new on the MyHeader header let hdr = MyHeader::new();
// Pass a data buffer as an argument
let hdr = MyHeader([0x00, 0x0a, 0x08, 0x10]);
make_header! generates helper methods and associated functions for each header and fields
rust
hdr.octets(); // get the vlan header as a byte array
println!("{}", hdr.field2()); // fetch the cfi field value
hdr.setfield2(1); // set the cfi field value
let hdrnew = hdr.clone(); // clone the packet
hdr.show(); // display the vlan header
Output of show(): Raw: 00 0a 08 00
field1 : 3 : 02 field2 : 1 : 01 field3 : 12 : 00 10 field4 : 16 : 08 00 ```
A packet is an ordered list of headers. Push headers as required into a packet. ```rust let mut pkt = Packet::new(100); pkt.push(Ether::new())); pkt.push(IPv4::new()); pkt.show()
dst : 48 : 00 01 02 03 04 05 src : 48 : 00 06 07 08 09 0a etype : 16 : 08 00
version : 4 : 04 ihl : 4 : 05 diffserv : 8 : 00 totallen : 16 : 00 14 identification : 16 : 00 33 flags : 3 : 02 fragstartset : 13 : 06 29 ttl : 8 : 64 protocol : 8 : 06 header_checksum : 16 : fa ec src : 32 : c0 a8 00 01 dst : 32 : c0 a8 00 02
// access ethernet header immutable
let x: &Ether
// access ethernet header mutable
let x: &mut Ether