3 releases

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

#472 in Encoding

Download history 3/week @ 2025-12-26 124/week @ 2026-01-02 97/week @ 2026-01-09 216/week @ 2026-01-16 287/week @ 2026-01-23 348/week @ 2026-01-30 232/week @ 2026-02-06 388/week @ 2026-02-13 199/week @ 2026-02-20 301/week @ 2026-02-27 464/week @ 2026-03-06 221/week @ 2026-03-13 210/week @ 2026-03-20 124/week @ 2026-03-27 89/week @ 2026-04-03 344/week @ 2026-04-10

810 downloads per month
Used in untrustended

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