1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
use std::{
io::{Result, Write},
net::Ipv4Addr,
};
use crate::wire;
const MAGIC_COOKIE: [u8; 4] = [0x63, 0x82, 0x53, 0x63];
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BootOp {
#[default]
Request,
Reply,
}
impl BootOp {
pub const OP_REQUEST: u8 = 1;
pub const OP_REPLY: u8 = 2;
}
impl From<BootOp> for u8 {
fn from(value: BootOp) -> Self {
match value {
BootOp::Request => BootOp::OP_REQUEST,
BootOp::Reply => BootOp::OP_REPLY,
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum HardwareType {
#[default]
Ethernet,
}
impl HardwareType {
pub const TYPE_ETHER: u8 = 1;
pub const LEN_ETHER: u8 = 6;
pub fn hardware_len(&self) -> u8 {
match self {
HardwareType::Ethernet => Self::LEN_ETHER,
}
}
}
impl From<HardwareType> for u8 {
fn from(value: HardwareType) -> Self {
match value {
HardwareType::Ethernet => HardwareType::TYPE_ETHER,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DhcpMessageType {
Ack,
}
impl DhcpMessageType {
pub const CODE_ACK: u8 = 5;
pub fn code(&self) -> u8 {
match self {
DhcpMessageType::Ack => Self::CODE_ACK,
}
}
}
#[derive(Debug, Clone)]
pub enum DhcpOption {
Pad,
End,
MessageType(DhcpMessageType),
ServerIdentifier(Ipv4Addr),
VendorClassIdentifier(String),
TftpServerName(String),
TftpFileName(String),
UserClassInformation(String),
ClientMachineIdentifier(String),
Unknown { code: u8, data: Vec<u8> },
}
impl DhcpOption {
pub const CODE_PAD: u8 = 0;
pub const CODE_END: u8 = 255;
pub const CODE_DHCP_MESSAGE_TYPE: u8 = 53;
pub const CODE_DHCP_SERVER_IDENTIFIER: u8 = 54;
pub const CODE_VENDOR_CLASS_IDENTIFIER: u8 = 60;
pub const CODE_TFTP_SERVER_NAME: u8 = 66;
pub const CODE_TFTP_FILE_NAME: u8 = 67;
pub const CODE_USER_CLASS_INFORMATION: u8 = 77;
pub const CODE_CLIENT_MACHINE_IDENTIFIER: u8 = 97;
pub fn code(&self) -> u8 {
match self {
DhcpOption::Pad => Self::CODE_PAD,
DhcpOption::End => Self::CODE_END,
DhcpOption::MessageType(_) => Self::CODE_DHCP_MESSAGE_TYPE,
DhcpOption::ServerIdentifier(_) => Self::CODE_DHCP_SERVER_IDENTIFIER,
DhcpOption::VendorClassIdentifier(_) => Self::CODE_VENDOR_CLASS_IDENTIFIER,
DhcpOption::TftpServerName(_) => Self::CODE_TFTP_SERVER_NAME,
DhcpOption::TftpFileName(_) => Self::CODE_TFTP_FILE_NAME,
DhcpOption::UserClassInformation(_) => Self::CODE_USER_CLASS_INFORMATION,
DhcpOption::ClientMachineIdentifier(_) => Self::CODE_CLIENT_MACHINE_IDENTIFIER,
DhcpOption::Unknown { code, .. } => *code,
}
}
}
#[derive(Debug)]
pub struct DhcpPacket {
pub op: BootOp,
pub htype: HardwareType,
pub xid: u32,
pub secs: u16,
pub flags: u16,
pub ciaddr: Ipv4Addr,
pub yiaddr: Ipv4Addr,
pub siaddr: Ipv4Addr,
pub giaddr: Ipv4Addr,
pub chaddr: [u8; 16],
// server host name
pub sname: Option<String>,
// boot file name
pub file: Option<String>,
pub options: Vec<DhcpOption>,
}
impl Default for DhcpPacket {
fn default() -> Self {
Self {
op: Default::default(),
htype: Default::default(),
xid: Default::default(),
secs: Default::default(),
flags: Default::default(),
ciaddr: Ipv4Addr::UNSPECIFIED,
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: Default::default(),
sname: Default::default(),
file: Default::default(),
options: Default::default(),
}
}
}
pub fn write_packet<W: Write>(mut writer: W, packet: &DhcpPacket) -> Result<()> {
wire::write_u8(&mut writer, u8::from(packet.op))?;
wire::write_u8(&mut writer, u8::from(packet.htype))?;
wire::write_u8(&mut writer, packet.htype.hardware_len())?;
wire::write_u8(&mut writer, 0)?; // hops
wire::write_u32(&mut writer, packet.xid)?;
wire::write_u16(&mut writer, packet.secs)?;
wire::write_u16(&mut writer, packet.flags)?;
wire::write_ipv4(&mut writer, packet.ciaddr)?;
wire::write_ipv4(&mut writer, packet.yiaddr)?;
wire::write_ipv4(&mut writer, packet.siaddr)?;
wire::write_ipv4(&mut writer, packet.giaddr)?;
wire::write(&mut writer, &packet.chaddr)?;
match &packet.sname {
Some(name) => wire::write_null_terminated_string(&mut writer, &name)?,
None => wire::write_null_terminated_string(&mut writer, "")?,
};
match &packet.file {
Some(name) => wire::write_null_terminated_string(&mut writer, &name)?,
None => wire::write_null_terminated_string(&mut writer, "")?,
};
wire::write(&mut writer, &MAGIC_COOKIE)?;
for option in &packet.options {
write_option(&mut writer, option)?;
}
write_option(&mut writer, &DhcpOption::End)?;
Ok(())
}
pub fn write_option<W: Write>(mut writer: W, option: &DhcpOption) -> Result<()> {
wire::write_u8(&mut writer, option.code())?;
match option {
DhcpOption::Pad | DhcpOption::End => {}
DhcpOption::MessageType(t) => {
wire::write_u8(&mut writer, 1)?;
wire::write_u8(&mut writer, t.code())?;
}
DhcpOption::ServerIdentifier(ip) => {
wire::write_u8(&mut writer, 4)?;
wire::write_ipv4(&mut writer, *ip)?;
}
DhcpOption::VendorClassIdentifier(vendor_class) => {
write_option_len_prefixed_string(&mut writer, &vendor_class)?
}
DhcpOption::TftpServerName(name) => write_option_len_prefixed_string(&mut writer, &name)?,
DhcpOption::TftpFileName(name) => write_option_len_prefixed_string(&mut writer, &name)?,
DhcpOption::UserClassInformation(user_class) => {
write_option_len_prefixed_string(&mut writer, &user_class)?
}
DhcpOption::ClientMachineIdentifier(identifier) => {
write_option_len_prefixed_string(&mut writer, &identifier)?
}
DhcpOption::Unknown { data, .. } => {
wire::write_u8(&mut writer, u8::try_from(data.len()).unwrap())?;
wire::write(&mut writer, &data)?;
}
}
Ok(())
}
fn write_option_len_prefixed_string<W: Write>(mut writer: W, s: &str) -> Result<()> {
wire::write_u8(&mut writer, u8::try_from(s.len()).unwrap())?;
wire::write(&mut writer, s.as_bytes())
}
|