dhcparse

Crates.iodhcparse
lib.rsdhcparse
version1.0.0
sourcesrc
created_at2021-11-16 18:37:54.541974
updated_at2022-09-13 14:44:18.356908
descriptionA zero-copy DHCPv4 parser
homepagehttps://github.com/axelf4/dhcparse
repositoryhttps://github.com/axelf4/dhcparse
max_upload_size
id482927
size84,475
Axel Forsman (axelf4)

documentation

README

dhcparse

A zero-copy DHCPv4 parser.

crates.io docs.rs

This crate is especially suitable for writing DHCP relay agents, which only need to read and write a few fields, set and possibly remove a couple of options, before forwarding an incoming DHCP message.

Examples

Basic usage:

use dhcparse::{v4_options, dhcpv4::{Message, MessageType}};
use std::net::Ipv4Addr;

let mut msg = Message::new(EXAMPLE_DISCOVER_MSG)?;

// Read a field
assert_eq!(msg.chaddr()?, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);

// Set a field
*msg.giaddr_mut() = Ipv4Addr::new(192, 168, 1, 50).into();

// Parse a set of options
assert_eq!(
    v4_options!(msg; MessageType required, ServerIdentifier, RequestedIpAddress)?,
    (
        MessageType::DISCOVER,
        None,
        Some(&Ipv4Addr::new(192, 168, 1, 100).into())
    )
);

Constructing a new message:

use dhcparse::dhcpv4::{DhcpOption, Encode as _, Encoder, Message, MessageType, OpCode};
// Create a copy of an empty message with the message type option added
let mut msg = Encoder
    .append_option(DhcpOption::MessageType(MessageType::DISCOVER))
    .encode_to_owned(&Message::default())?;
msg.set_op(OpCode::BootRequest);

assert_eq!(msg.options()?.count(), 1);

Related projects

  • dhcproto is another Rust crate that instead parses DHCP messages into an owned representation. This may be more convenient for some applications, but comes with the overhead of copying and heap allocations. For instance, the current set of dhcproto benchmarks would be no-ops with dhcparse.
Commit count: 38

cargo fmt