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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#![allow(missing_debug_implementations)]
use super::ClientConfig;
use crate::v4::message::*;
use mac_address::get_mac_address;
use rand::RngCore;
use rand::rngs::ThreadRng;
use std::net::Ipv4Addr;
fn get_chaddr() -> [u8; 16] {
let address = get_mac_address().unwrap().unwrap().bytes();
let mut chaddr: [u8; 16] = [0; 16];
chaddr[..6].copy_from_slice(&address[..6]);
chaddr
}
/// Logic for a DHCP client using this lib.
///
/// Used by the client binary and integration tests.
///
/// Requires the `v4_client` feature, which is enabled by default.
pub struct Client {
/// A UdpSocket that understands Deliverables to communicate with the Server
pub socket: Socket,
/// The client's hardware address.
///
/// This is cached for use in multiple messages to the server,
/// and the server will reuse it in its replies.
pub haddr: [u8; 16],
/// The random number generator used to generate the number for xid.
///
/// Defaults to `ThreadRng`, but can be set to a custom value
/// with `Client.set_rng()`.
rng: Box<dyn RngCore>,
// TODO store current lease here
}
impl Client {
/// A wrapper around [UdpSocket::bind](https://doc.rust-lang.org/std/net/struct.UdpSocket.html#method.bind)
pub fn new(config: &ClientConfig) -> Self {
Self {
socket: Socket::new(config.listen_address.unwrap(), config.interface.as_ref()),
haddr: get_chaddr(),
rng: Box::new(ThreadRng::default()),
}
}
/// Change the default random number generator from [ThreadRng](https://docs.rs/rand/latest/rand/rngs/struct.ThreadRng.html) to any rng that implements RngCore from the [rand](https://docs.rs/rand/latest/rand/rngs/index.html) crate.
///
/// For example, you could use [SmallRng](https://docs.rs/rand/latest/rand/rngs/struct.SmallRng.html)
/// which is sometimes faster, but has a higher collision rate of 1 in 65,000:
/// ```
/// use toe_beans::v4::{Client, ClientConfig};
/// use std::net::{Ipv4Addr, SocketAddrV4};
/// use rand::SeedableRng;
/// use rand::rngs::SmallRng;
///
/// let client_config = ClientConfig::default();
/// let mut client = Client::new(&client_config);
///
/// // initialize the random number generator once.
/// let rng = SmallRng::from_os_rng();
///
/// client.set_rng(Box::new(rng));
/// ```
pub fn set_rng(&mut self, rng: Box<dyn RngCore>) {
self.rng = rng;
}
/// Generates a number for xid by using the cached, initialized rng.
/// Note that generating a number mutates the internal state of the rng.
fn get_xid(&mut self) -> u32 {
self.rng.next_u32()
}
/// Sent from the client to server as the start of a DHCP conversation.
pub fn discover(&mut self) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: self.get_xid(),
secs: 0,
flags: Flags { broadcast: false }, // TODO or true if required
ciaddr: Ipv4Addr::UNSPECIFIED,
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Discover),
// TODO may set option 50 to request an ip address
]
.into(),
}
}
/// Sent from client to server (during RENEWING/REBINDING) when the client has a lease but wants to renew or rebind it.
///
/// A renew will unicast, whereas a rebind will broadcast.
/// Rebinding is intended for multiple DHCP servers that can coordinate consistenency of their leases
pub fn extend(&mut self, address: Ipv4Addr) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: self.get_xid(),
secs: 0,
flags: Flags { broadcast: false },
ciaddr: address, // MUST be client's IP address
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Request),
// MUST NOT fill in requested ip address
// MUST NOT fill in server identifier
]
.into(),
}
}
/// Broadcast from client to server (during INIT-REBOOT) to verify a previously allocated, cached configuration.
// Server SHOULD send a DHCPNAK message to the client if the 'requested IP address'
// is incorrect, or is on the wrong network
pub fn verify(&mut self, address: Ipv4Addr) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: self.get_xid(),
secs: 0,
flags: Flags { broadcast: false },
ciaddr: Ipv4Addr::UNSPECIFIED, // MUST be zero
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Request),
MessageOptions::RequestedIp(AddressOption::new(address)), // MUST be filled in with previously assigned address
// MUST NOT fill in server identifier
]
.into(),
}
}
/// Broadcast from the client to server (during SELECTING) following an Offer message.
pub fn request(&self, message: Message) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: message.xid,
secs: 0,
flags: Flags { broadcast: false }, // TODO or true if required
ciaddr: Ipv4Addr::UNSPECIFIED, // MUST be zero
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: message.chaddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Request),
MessageOptions::RequestedIp(AddressOption::new(message.yiaddr)), // MUST be filled in
MessageOptions::ServerIdentifier(AddressOption::new(message.siaddr)), // MUST be filled in
]
.into(),
}
}
/// Sent from the client to server (following an Ack)
/// if the IP address is already in use.
pub fn decline(&self, message: Message) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: message.xid,
secs: 0,
flags: Flags { broadcast: false },
ciaddr: Ipv4Addr::UNSPECIFIED,
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Decline),
MessageOptions::RequestedIp(AddressOption::new(message.yiaddr)), // MUST
MessageOptions::ServerIdentifier(AddressOption::new(message.siaddr)), // MUST
// Must not:
// - Requested IP address
// - IP address lease time
// - Vendor class identifier
// - Parameter request list
// - Maximum message size
// - Site-specific
// - All others (than: use file/sname, client identifier, and message)
]
.into(),
}
}
/// Sent from the client to server.
pub fn release(&mut self) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: self.get_xid(),
secs: 0,
flags: Flags { broadcast: false },
ciaddr: Ipv4Addr::UNSPECIFIED, // TODO client's network address
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Release),
MessageOptions::ServerIdentifier(AddressOption::new(Ipv4Addr::UNSPECIFIED)), // MUST, TODO server's address
// Must not:
// - Requested IP address
// - IP address lease time
// - Vendor class identifier
// - Parameter request list
// - Maximum message size
// - Site-specific
// - All others (than: use file/sname, client identifier, and message)
]
.into(),
}
}
/// Sent from the client to server.
pub fn inform(&mut self, parameter_list: Vec<u8>) -> Message {
Message {
op: Ops::Request,
htype: HTypes::Ethernet,
hlen: 6,
hops: 0,
xid: self.get_xid(),
secs: 0,
flags: Flags { broadcast: false },
ciaddr: Ipv4Addr::UNSPECIFIED, // TODO client's network address
yiaddr: Ipv4Addr::UNSPECIFIED,
siaddr: Ipv4Addr::UNSPECIFIED,
giaddr: Ipv4Addr::UNSPECIFIED,
chaddr: self.haddr,
sname: SName::EMPTY,
file: File::EMPTY,
magic: MAGIC,
options: vec![
MessageOptions::MessageType(MessageTypes::Inform),
MessageOptions::RequestedOptions(parameter_list),
]
.into(),
}
}
}