13个版本 (破坏性更新)
0.12.0 | 2024年6月7日 |
---|---|
0.11.0 | 2023年11月6日 |
0.10.0 | 2023年9月29日 |
0.9.0 | 2023年3月6日 |
0.1.0 |
|
#87 in 解析器实现
每月25,053次 下载
在 5 个crate中使用 (3 个直接使用)
235KB
5K SLoC
dhcproto
一个用于DHCPv4/DHCPv6的DHCP解析器和编码器。 dhcproto
旨在成为一个功能完整的DHCP实现。实现了许多常见的选项类型,欢迎提交PR来完善缺失的类型。
注意! 我们正在使用这个库开发一个名为dora的DHCP服务器!
特性
- v4是100%安全的Rust (v6在检查边界后使用
get_unchecked
) - v4 & v6消息类型
- v4 & v6消息头获取/设置器,所有数据可变
- 支持100多种完全类型安全的选项类型(接受未知类型的PR)
- 支持长选项编码(RFC 3396)(允许编码超过255字节的选项)
- 基准编码/解码
crates.io
https://crates.io/crates/dhcproto
最小Rust版本
此crate使用const generics,需要Rust 1.53
示例
(v4) 解码/编码
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) 构建消息
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
支持的RFC
DHCPv6
- https://datatracker.ietf.org/doc/html/rfc8415
- https://datatracker.ietf.org/doc/html/rfc3646
- https://datatracker.ietf.org/doc/html/rfc3633
- https://datatracker.ietf.org/doc/html/rfc5007 (仅消息类型)
- https://datatracker.ietf.org/doc/html/rfc5908
- https://datatracker.ietf.org/doc/html/rfc5460 (仅消息类型/状态码,没有opt 53)
- https://datatracker.ietf.org/doc/html/rfc6977 (仅消息类型)
- https://datatracker.ietf.org/doc/html/rfc7341 (仅消息类型)
DHCPv4
- https://tools.ietf.org/html/rfc2131
- https://tools.ietf.org/html/rfc3011
- https://tools.ietf.org/html/rfc3232
- https://tools.ietf.org/html/rfc3203
- https://tools.ietf.org/html/rfc3046
- https://tools.ietf.org/html/rfc3396
- https://tools.ietf.org/html/rfc3397
- https://tools.ietf.org/html/rfc4039
- https://tools.ietf.org/html/rfc4280
- https://tools.ietf.org/html/rfc4388(消息类型 & 选项)
- https://tools.ietf.org/html/rfc4578
- https://tools.ietf.org/html/rfc4702
- https://tools.ietf.org/html/rfc6926(消息类型 & 选项 151-157)
- https://tools.ietf.org/html/rfc7724(仅消息类型,选项 151 的状态码尚未实现)
- https://tools.ietf.org/html/rfc8910
- https://tools.ietf.org/html/rfc2563
- https://tools.ietf.org/html/rfc8925
依赖关系
~5.5MB
~124K SLoC