| Crates.io | pktgen |
| lib.rs | pktgen |
| version | 0.1.2 |
| created_at | 2025-05-31 06:38:23.107386+00 |
| updated_at | 2025-05-31 06:46:10.162505+00 |
| description | A modular network packet builder library |
| homepage | |
| repository | https://github.com/Scharxi/packet_builder |
| max_upload_size | |
| id | 1696097 |
| size | 111,741 |
A modular network packet builder library in Rust for creating, manipulating, and serializing network packets.
Ethernet (IEEE 802.3)
IPv4
TCP
UDP
ICMP
ARP
DHCP
use packet_builder::ethernet::{EthernetPacket, MacAddress, EtherType};
let src_mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
let dst_mac = MacAddress::new([0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB]);
let packet = EthernetPacket::builder()
.src_mac(src_mac)
.dst_mac(dst_mac)
.ether_type(EtherType::IPv4)
.payload(vec![1, 2, 3, 4])
.build()
.unwrap();
let bytes = packet.build().unwrap();
use packet_builder::ip::{Ipv4Packet, Ipv4Address, IpProtocol};
let src_ip = Ipv4Address::new([192, 168, 1, 1]);
let dst_ip = Ipv4Address::new([192, 168, 1, 2]);
let packet = Ipv4Packet::builder()
.protocol(IpProtocol::TCP)
.src_addr(src_ip)
.dst_addr(dst_ip)
.payload(vec![1, 2, 3, 4])
.build()
.unwrap();
let bytes = packet.build().unwrap();
use packet_builder::tcp::{TcpPacket, TcpFlags};
let mut flags = TcpFlags::new();
flags.syn = true;
let packet = TcpPacket::builder()
.src_port(12345)
.dst_port(80)
.sequence(1000)
.flags(flags)
.payload(vec![1, 2, 3, 4])
.build()
.unwrap();
let bytes = packet.build().unwrap();
use packet_builder::udp::UdpPacket;
let packet = UdpPacket::builder()
.src_port(12345)
.dst_port(53)
.payload(vec![1, 2, 3, 4])
.build()
.unwrap();
let bytes = packet.build().unwrap();
use packet_builder::icmp::{IcmpPacket, IcmpType};
let packet = IcmpPacket::echo_request(1, 1, b"Hello, World!".to_vec()).unwrap();
let bytes = packet.build().unwrap();
use packet_builder::arp::ArpPacket;
use packet_builder::ethernet::MacAddress;
use packet_builder::ip::Ipv4Address;
let src_mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
let src_ip = Ipv4Address::new([192, 168, 1, 1]);
let target_ip = Ipv4Address::new([192, 168, 1, 2]);
let packet = ArpPacket::request(src_mac, src_ip, target_ip).unwrap();
let bytes = packet.build().unwrap();
use packet_builder::dhcp::{DhcpPacket, MessageType};
use packet_builder::ethernet::MacAddress;
let chaddr = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
let xid = 0x12345678;
let packet = DhcpPacket::discover(xid, chaddr).unwrap();
let bytes = packet.build().unwrap();
The library uses a custom error type PacketError for handling various error conditions:
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.