Packet Builder
A modular network packet builder library in Rust for creating, manipulating, and serializing network packets.
Features
- Modular design with separate modules for different protocols
- Support for Ethernet, IPv4, TCP, UDP, ICMP, ARP, and DHCP packets
- Type-safe packet construction with validation
- Checksum calculation and verification
- Extensible architecture through traits
- Serialization support using serde
Supported Protocols
-
Ethernet (IEEE 802.3)
- MAC addressing
- EtherType selection
- Payload encapsulation
-
IPv4
- Address handling
- Protocol selection
- Flags and fragmentation
- Header checksum calculation
-
TCP
- Port management
- Sequence and acknowledgment numbers
- Flags (SYN, ACK, FIN, etc.)
- Window size
- Options support
- Checksum calculation
-
UDP
- Port management
- Length calculation
- Checksum calculation
-
ICMP
- Message types (Echo, Destination Unreachable, etc.)
- Code values
- Sequence numbers
- Identifier values
- Checksum calculation
-
ARP
- Hardware types
- Protocol types
- Operation codes (Request/Reply)
- Address resolution
- Hardware and protocol address handling
-
DHCP
- Message types (Discover, Offer, Request, etc.)
- Options handling
- Address assignment
- Configuration parameters
- Client/Server communication
Usage Examples
Creating an Ethernet Packet
use ;
let src_mac = new;
let dst_mac = new;
let packet = builder
.src_mac
.dst_mac
.ether_type
.payload
.build
.unwrap;
let bytes = packet.build.unwrap;
Creating an IPv4 Packet
use ;
let src_ip = new;
let dst_ip = new;
let packet = builder
.protocol
.src_addr
.dst_addr
.payload
.build
.unwrap;
let bytes = packet.build.unwrap;
Creating a TCP Packet
use ;
let mut flags = new;
flags.syn = true;
let packet = builder
.src_port
.dst_port
.sequence
.flags
.payload
.build
.unwrap;
let bytes = packet.build.unwrap;
Creating a UDP Packet
use UdpPacket;
let packet = builder
.src_port
.dst_port
.payload
.build
.unwrap;
let bytes = packet.build.unwrap;
Creating an ICMP Echo Request Packet
use ;
let packet = echo_request.unwrap;
let bytes = packet.build.unwrap;
Creating an ARP Request Packet
use ArpPacket;
use MacAddress;
use Ipv4Address;
let src_mac = new;
let src_ip = new;
let target_ip = new;
let packet = request.unwrap;
let bytes = packet.build.unwrap;
Creating a DHCP Discover Packet
use ;
use MacAddress;
let chaddr = new;
let xid = 0x12345678;
let packet = discover.unwrap;
let bytes = packet.build.unwrap;
Error Handling
The library uses a custom error type PacketError for handling various error conditions:
- Invalid packet length
- Invalid checksum
- Serialization errors
- Invalid field values
- Buffer size issues
- Protocol version mismatches
- Header format errors
- Unsupported protocol errors
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.