Crates.io | dhcparse |
lib.rs | dhcparse |
version | 1.0.0 |
source | src |
created_at | 2021-11-16 18:37:54.541974 |
updated_at | 2022-09-13 14:44:18.356908 |
description | A zero-copy DHCPv4 parser |
homepage | https://github.com/axelf4/dhcparse |
repository | https://github.com/axelf4/dhcparse |
max_upload_size | |
id | 482927 |
size | 84,475 |
A zero-copy DHCPv4 parser.
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.
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);