@@ -426,25 +426,30 @@ def _dhcp_state_machine(self) -> None:
426
426
except ValueError as error :
427
427
if self ._debug :
428
428
print (error )
429
- if msg_type == DHCP_OFFER :
430
- # Check if transaction ID matches, otherwise it may be an offer
431
- # for another device
432
- if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
433
- if self ._debug :
434
- print (
435
- "* DHCP: Send request to {}" .format (self .dhcp_server_ip )
429
+ else :
430
+ if msg_type == DHCP_OFFER :
431
+ # Check if transaction ID matches, otherwise it may be an offer
432
+ # for another device
433
+ if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
434
+ if self ._debug :
435
+ print (
436
+ "* DHCP: Send request to {}" .format (
437
+ self .dhcp_server_ip
438
+ )
439
+ )
440
+ self ._transaction_id = (
441
+ self ._transaction_id + 1
442
+ ) & 0x7FFFFFFF
443
+ self .send_dhcp_message (
444
+ DHCP_REQUEST , (time .monotonic () - self ._start_time )
436
445
)
437
- self ._transaction_id = (self ._transaction_id + 1 ) & 0x7FFFFFFF
438
- self .send_dhcp_message (
439
- DHCP_REQUEST , (time .monotonic () - self ._start_time )
440
- )
441
- self ._dhcp_state = STATE_DHCP_REQUEST
446
+ self ._dhcp_state = STATE_DHCP_REQUEST
447
+ else :
448
+ if self ._debug :
449
+ print ("* DHCP: Received OFFER with non-matching xid" )
442
450
else :
443
451
if self ._debug :
444
- print ("* DHCP: Received OFFER with non-matching xid" )
445
- else :
446
- if self ._debug :
447
- print ("* DHCP: Received DHCP Message is not OFFER" )
452
+ print ("* DHCP: Received DHCP Message is not OFFER" )
448
453
449
454
elif self ._dhcp_state == STATE_DHCP_REQUEST :
450
455
if self ._sock .available ():
@@ -455,39 +460,40 @@ def _dhcp_state_machine(self) -> None:
455
460
except ValueError as error :
456
461
if self ._debug :
457
462
print (error )
458
- # Check if transaction ID matches, otherwise it may be
459
- # for another device
460
- if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
461
- if msg_type == DHCP_ACK :
462
- if self ._debug :
463
- print ("* DHCP: Successful lease" )
464
- self ._sock .close ()
465
- self ._sock = None
466
- self ._dhcp_state = STATE_DHCP_LEASED
467
- self ._last_lease_time = self ._start_time
468
- if self ._lease_time == 0 :
469
- self ._lease_time = DEFAULT_LEASE_TIME
470
- if self ._t1 == 0 :
471
- # T1 is 50% of _lease_time
472
- self ._t1 = self ._lease_time >> 1
473
- if self ._t2 == 0 :
474
- # T2 is 87.5% of _lease_time
475
- self ._t2 = self ._lease_time - (self ._lease_time >> 3 )
476
- self ._renew_in_sec = self ._t1
477
- self ._rebind_in_sec = self ._t2
478
- self ._eth .ifconfig = (
479
- self .local_ip ,
480
- self .subnet_mask ,
481
- self .gateway_ip ,
482
- self .dns_server_ip ,
483
- )
484
- gc .collect ()
463
+ else :
464
+ # Check if transaction ID matches, otherwise it may be
465
+ # for another device
466
+ if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
467
+ if msg_type == DHCP_ACK :
468
+ if self ._debug :
469
+ print ("* DHCP: Successful lease" )
470
+ self ._sock .close ()
471
+ self ._sock = None
472
+ self ._dhcp_state = STATE_DHCP_LEASED
473
+ self ._last_lease_time = self ._start_time
474
+ if self ._lease_time == 0 :
475
+ self ._lease_time = DEFAULT_LEASE_TIME
476
+ if self ._t1 == 0 :
477
+ # T1 is 50% of _lease_time
478
+ self ._t1 = self ._lease_time >> 1
479
+ if self ._t2 == 0 :
480
+ # T2 is 87.5% of _lease_time
481
+ self ._t2 = self ._lease_time - (self ._lease_time >> 3 )
482
+ self ._renew_in_sec = self ._t1
483
+ self ._rebind_in_sec = self ._t2
484
+ self ._eth .ifconfig = (
485
+ self .local_ip ,
486
+ self .subnet_mask ,
487
+ self .gateway_ip ,
488
+ self .dns_server_ip ,
489
+ )
490
+ gc .collect ()
491
+ else :
492
+ if self ._debug :
493
+ print ("* DHCP: Received DHCP Message is not ACK" )
485
494
else :
486
495
if self ._debug :
487
- print ("* DHCP: Received DHCP Message is not ACK" )
488
- else :
489
- if self ._debug :
490
- print ("* DHCP: Received non-matching xid" )
496
+ print ("* DHCP: Received non-matching xid" )
491
497
492
498
elif self ._dhcp_state == STATE_DHCP_WAIT :
493
499
if time .monotonic () > (self ._start_time + DHCP_WAIT_TIME ):
0 commit comments