3 releases

new 0.1.2 Feb 9, 2026
0.1.1 Apr 15, 2025
0.1.0 Feb 18, 2025

#464 in Encoding

Download history 196/week @ 2025-10-20 244/week @ 2025-10-27 170/week @ 2025-11-03 81/week @ 2025-11-10 198/week @ 2025-11-17 408/week @ 2025-11-24 184/week @ 2025-12-01 249/week @ 2025-12-08 106/week @ 2025-12-15 55/week @ 2025-12-22 5/week @ 2025-12-29 149/week @ 2026-01-05 87/week @ 2026-01-12 296/week @ 2026-01-19 289/week @ 2026-01-26 260/week @ 2026-02-02

952 downloads per month

MIT license

59KB
1.5K SLoC

pktbuilder

A safe builder for building network packets.

A simple way to build packets is to give a buffer to [Builder] and use it's methods to populate buffer with values.

use std::net::Ipv4Addr;
use pktbuilder::Builder;

fn example1() -> Result<(), pktbuilder::Error> {
    let mut buffer = [0u8; 6];
    let mut builder = Builder::new(&mut buffer);

    let ip = Ipv4Addr::new(192, 0, 2, 42);

    builder
        .add_byte(0x01)?
        .add_ipv4_address_be(ip)?
        .add_byte(0x02)?;

    assert_eq!(buffer, [0x01_u8, 192, 0, 2, 42, 0x02]);
    Ok(())
}

But for most cases we already have e.g. a struct that should be encoded so we can implement [Buildable] trait for the struct and use Buildable::build method to write into [Builder]:

use std::net::Ipv4Addr;
use pktbuilder::{Builder, Buildable};

struct Foo {
    foo: u8,
    ip: Ipv4Addr,
    bar: u8,
}

impl Buildable for Foo {
    fn build(&self, builder: &mut Builder<'_>) -> Result<(), pktbuilder::Error> {
        builder
            .add_byte(self.foo)?
            .add_ipv4_address_be(self.ip)?
            .add_byte(self.bar)?;
        Ok(())
    }
}


fn example2() -> Result<(), pktbuilder::Error> {
    let mut buffer = [0u8; 6];
    let mut builder = Builder::new(&mut buffer);

    let foo = Foo {
        foo: 0x01,
        ip: Ipv4Addr::new(192, 0, 2, 42),
        bar: 0x02,
    };

    foo.build(&mut builder)?;

    assert_eq!(buffer, [0x01_u8, 192, 0, 2, 42, 0x02]);
    Ok(())
}

No runtime deps