#dhcp-server #dhcp #message-parser #dhcpv6 #dhcpv4 #codec

dhcproto

A DHCP parser and encoder for DHCPv4/DHCPv6. dhcproto aims to be a functionally complete DHCP implementation.

13 releases (breaking)

0.12.0 Jun 7, 2024
0.11.0 Nov 6, 2023
0.10.0 Sep 29, 2023
0.9.0 Mar 6, 2023
0.1.0 Jul 15, 2021

#1 in #dhcp

Download history 5713/week @ 2024-07-17 5164/week @ 2024-07-24 6660/week @ 2024-07-31 6314/week @ 2024-08-07 7678/week @ 2024-08-14 6880/week @ 2024-08-21 7021/week @ 2024-08-28 5095/week @ 2024-09-04 4918/week @ 2024-09-11 4612/week @ 2024-09-18 6966/week @ 2024-09-25 5296/week @ 2024-10-02 5715/week @ 2024-10-09 5113/week @ 2024-10-16 5128/week @ 2024-10-23 4765/week @ 2024-10-30

21,556 downloads per month
Used in 5 crates (3 directly)

MIT license

235KB
5K SLoC

dhcproto

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.

Attention! We are developing a DHCP server using this library called dora!

features

  • v4 is 100% safe rust (v6 uses get_unchecked after bounds have been checked)
  • v4 & v6 Message types
  • v4 & v6 message header getters/setters, all data mutable
  • option types with 100's of fully type safe variants (accepting PR's for unknown variants)
  • Long option encoding supported (RFC 3396) (allows encoding options longer than 255 bytes)
  • benchmarked encoding/decoding

crates.io

https://crates.io/crates/dhcproto

Minimum Rust Version

This crate uses const generics, Rust 1.53 is required

Examples

(v4) Decoding/Encoding

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)?;

(v4) Constructing messages

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.set_flags(v4::Flags::default().set_broadcast()) // set broadcast to true
    .set_chaddr(&chaddr) // set chaddr
    .opts_mut()
    .insert(v4::DhcpOption::MessageType(v4::MessageType::Discover)); // set msg type

// set some more options
msg.opts_mut()
    .insert(v4::DhcpOption::ParameterRequestList(vec![
        v4::OptionCode::SubnetMask,
        v4::OptionCode::Router,
        v4::OptionCode::DomainNameServer,
        v4::OptionCode::DomainName,
    ]));
msg.opts_mut()
    .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

RFCs supported

DHCPv6:

DHCPv4:

Dependencies

~8MB
~152K SLoC