A DHCP parser and encoder for DHCPv4/DHCPv6. dhcproto
aims to be a functionally complete DHCP implementation. Many common option types are implemented, PRs are welcome to flesh out missing types.
https://crates.io/crates/dhcproto
This crate uses const generics, Rust 1.53 is required
rust
use dhcproto::v4::{Message, Encoder, Decoder, Decodable, Encodable};
// decode
let bytes = dhcp_offer();
let msg = Message::decode(&mut Decoder::new(&bytes))?;
// now encode
let mut buf = Vec::new();
let mut e = Encoder::new(&mut buf);
msg.encode(&mut e)?;
```rust use dhcproto::{v4, Encodable, Encoder}; // hardware addr let chaddr = vec![ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, ]; // construct a new Message let mut msg = v4::Message::default(); msg.setflags(v4::Flags::default().setbroadcast()) // set broadcast to true .setchaddr(&chaddr) // set chaddr .optsmut() .insert(v4::DhcpOption::MessageType(v4::MessageType::Discover)); // set msg type
// set some more options msg.optsmut() .insert(v4::DhcpOption::ParameterRequestList(vec![ v4::OptionCode::SubnetMask, v4::OptionCode::Router, v4::OptionCode::DomainNameServer, v4::OptionCode::DomainName, ])); msg.optsmut() .insert(v4::DhcpOption::ClientIdentifier(chaddr));
// now encode to bytes let mut buf = Vec::new(); let mut e = Encoder::new(&mut buf); msg.encode(&mut e)?; // buf now has the contents of the encoded DHCP message ```
DHCPv6:
DHCPv4: