1
1
use crate :: { Error , Result } ;
2
- use crate :: wire:: { EthernetAddress , IpProtocol , IpAddress ,
2
+ use crate :: wire:: { IpProtocol , IpAddress ,
3
3
Ipv4Cidr , Ipv4Address , Ipv4Repr ,
4
4
UdpRepr , UDP_HEADER_LEN ,
5
5
DhcpPacket , DhcpRepr , DhcpMessageType , DHCP_CLIENT_PORT , DHCP_SERVER_PORT , DHCP_MAX_DNS_SERVER_COUNT } ;
6
6
use crate :: wire:: dhcpv4:: { field as dhcpv4_field} ;
7
- use crate :: socket:: SocketMeta ;
7
+ use crate :: socket:: { SocketMeta , Context } ;
8
8
use crate :: time:: { Instant , Duration } ;
9
9
use crate :: socket:: SocketHandle ;
10
10
@@ -150,7 +150,7 @@ impl Dhcpv4Socket {
150
150
self . max_lease_duration = max_lease_duration;
151
151
}
152
152
153
- pub ( crate ) fn poll_at ( & self ) -> PollAt {
153
+ pub ( crate ) fn poll_at ( & self , _cx : & Context ) -> PollAt {
154
154
let t = match & self . state {
155
155
ClientState :: Discovering ( state) => state. retry_at ,
156
156
ClientState :: Requesting ( state) => state. retry_at ,
@@ -159,7 +159,7 @@ impl Dhcpv4Socket {
159
159
PollAt :: Time ( t)
160
160
}
161
161
162
- pub ( crate ) fn process ( & mut self , now : Instant , ethernet_addr : EthernetAddress , ip_repr : & Ipv4Repr , repr : & UdpRepr , payload : & [ u8 ] ) -> Result < ( ) > {
162
+ pub ( crate ) fn process ( & mut self , cx : & Context , ip_repr : & Ipv4Repr , repr : & UdpRepr , payload : & [ u8 ] ) -> Result < ( ) > {
163
163
let src_ip = ip_repr. src_addr ;
164
164
165
165
// This is enforced in interface.rs.
@@ -179,7 +179,7 @@ impl Dhcpv4Socket {
179
179
return Ok ( ( ) ) ;
180
180
}
181
181
} ;
182
- if dhcp_repr. client_hardware_address != ethernet_addr { return Ok ( ( ) ) }
182
+ if dhcp_repr. client_hardware_address != cx . ethernet_address . unwrap ( ) { return Ok ( ( ) ) }
183
183
if dhcp_repr. transaction_id != self . transaction_id { return Ok ( ( ) ) }
184
184
let server_identifier = match dhcp_repr. server_identifier {
185
185
Some ( server_identifier) => server_identifier,
@@ -199,7 +199,7 @@ impl Dhcpv4Socket {
199
199
}
200
200
201
201
self . state = ClientState :: Requesting ( RequestState {
202
- retry_at : now,
202
+ retry_at : cx . now ,
203
203
retry : 0 ,
204
204
server : ServerInfo {
205
205
address : src_ip,
@@ -209,7 +209,7 @@ impl Dhcpv4Socket {
209
209
} ) ;
210
210
}
211
211
( ClientState :: Requesting ( state) , DhcpMessageType :: Ack ) => {
212
- if let Some ( ( config, renew_at, expires_at) ) = Self :: parse_ack ( now, & dhcp_repr, self . max_lease_duration ) {
212
+ if let Some ( ( config, renew_at, expires_at) ) = Self :: parse_ack ( cx . now , & dhcp_repr, self . max_lease_duration ) {
213
213
self . config_changed = true ;
214
214
self . state = ClientState :: Renewing ( RenewState {
215
215
server : state. server ,
@@ -223,7 +223,7 @@ impl Dhcpv4Socket {
223
223
self . reset ( ) ;
224
224
}
225
225
( ClientState :: Renewing ( state) , DhcpMessageType :: Ack ) => {
226
- if let Some ( ( config, renew_at, expires_at) ) = Self :: parse_ack ( now, & dhcp_repr, self . max_lease_duration ) {
226
+ if let Some ( ( config, renew_at, expires_at) ) = Self :: parse_ack ( cx . now , & dhcp_repr, self . max_lease_duration ) {
227
227
state. renew_at = renew_at;
228
228
state. expires_at = expires_at;
229
229
if state. config != config {
@@ -298,9 +298,13 @@ impl Dhcpv4Socket {
298
298
Some ( ( config, renew_at, expires_at) )
299
299
}
300
300
301
- pub ( crate ) fn dispatch < F > ( & mut self , now : Instant , ethernet_addr : EthernetAddress , ip_mtu : usize , emit : F ) -> Result < ( ) >
301
+ pub ( crate ) fn dispatch < F > ( & mut self , cx : & Context , emit : F ) -> Result < ( ) >
302
302
where F : FnOnce ( ( Ipv4Repr , UdpRepr , DhcpRepr ) ) -> Result < ( ) > {
303
303
304
+ // note: Dhcpv4Socket is only usable in ethernet mediums, so the
305
+ // unwrap can never fail.
306
+ let ethernet_addr = cx. ethernet_address . unwrap ( ) ;
307
+
304
308
// Worst case biggest IPv4 header length.
305
309
// 0x0f * 4 = 60 bytes.
306
310
const MAX_IPV4_HEADER_LEN : usize = 60 ;
@@ -324,7 +328,7 @@ impl Dhcpv4Socket {
324
328
client_identifier : Some ( ethernet_addr) ,
325
329
server_identifier : None ,
326
330
parameter_request_list : Some ( PARAMETER_REQUEST_LIST ) ,
327
- max_size : Some ( ( ip_mtu - MAX_IPV4_HEADER_LEN - UDP_HEADER_LEN ) as u16 ) ,
331
+ max_size : Some ( ( cx . caps . ip_mtu ( ) - MAX_IPV4_HEADER_LEN - UDP_HEADER_LEN ) as u16 ) ,
328
332
lease_duration : None ,
329
333
dns_servers : None ,
330
334
} ;
@@ -344,7 +348,7 @@ impl Dhcpv4Socket {
344
348
345
349
match & mut self . state {
346
350
ClientState :: Discovering ( state) => {
347
- if now < state. retry_at {
351
+ if cx . now < state. retry_at {
348
352
return Err ( Error :: Exhausted )
349
353
}
350
354
@@ -354,12 +358,12 @@ impl Dhcpv4Socket {
354
358
emit ( ( ipv4_repr, udp_repr, dhcp_repr) ) ?;
355
359
356
360
// Update state AFTER the packet has been successfully sent.
357
- state. retry_at = now + DISCOVER_TIMEOUT ;
361
+ state. retry_at = cx . now + DISCOVER_TIMEOUT ;
358
362
self . transaction_id = next_transaction_id;
359
363
Ok ( ( ) )
360
364
}
361
365
ClientState :: Requesting ( state) => {
362
- if now < state. retry_at {
366
+ if cx . now < state. retry_at {
363
367
return Err ( Error :: Exhausted )
364
368
}
365
369
@@ -380,21 +384,21 @@ impl Dhcpv4Socket {
380
384
emit ( ( ipv4_repr, udp_repr, dhcp_repr) ) ?;
381
385
382
386
// Exponential backoff: Double every 2 retries.
383
- state. retry_at = now + ( REQUEST_TIMEOUT << ( state. retry as u32 / 2 ) ) ;
387
+ state. retry_at = cx . now + ( REQUEST_TIMEOUT << ( state. retry as u32 / 2 ) ) ;
384
388
state. retry += 1 ;
385
389
386
390
self . transaction_id = next_transaction_id;
387
391
Ok ( ( ) )
388
392
}
389
393
ClientState :: Renewing ( state) => {
390
- if state. expires_at <= now {
394
+ if state. expires_at <= cx . now {
391
395
net_debug ! ( "DHCP lease expired" ) ;
392
396
self . reset ( ) ;
393
397
// return Ok so we get polled again
394
398
return Ok ( ( ) )
395
399
}
396
400
397
- if now < state. renew_at {
401
+ if cx . now < state. renew_at {
398
402
return Err ( Error :: Exhausted )
399
403
}
400
404
@@ -413,7 +417,7 @@ impl Dhcpv4Socket {
413
417
// of the remaining time until T2 (in RENEWING state) and one-half of
414
418
// the remaining lease time (in REBINDING state), down to a minimum of
415
419
// 60 seconds, before retransmitting the DHCPREQUEST message.
416
- state. renew_at = now + MIN_RENEW_TIMEOUT . max ( ( state. expires_at - now) / 2 ) ;
420
+ state. renew_at = cx . now + MIN_RENEW_TIMEOUT . max ( ( state. expires_at - cx . now ) / 2 ) ;
417
421
418
422
self . transaction_id = next_transaction_id;
419
423
Ok ( ( ) )
0 commit comments