33
33
34
34
35
35
# DHCP State Machine
36
- STATE_DHCP_START = const (0x00 )
37
- STATE_DHCP_DISCOVER = const (0x01 )
38
- STATE_DHCP_REQUEST = const (0x02 )
39
- STATE_DHCP_LEASED = const (0x03 )
40
- STATE_DHCP_REREQUEST = const (0x04 )
41
- STATE_DHCP_RELEASE = const (0x05 )
42
- STATE_DHCP_WAIT = const (0x06 )
43
- STATE_DHCP_DISCONN = const (0x07 )
36
+ _STATE_DHCP_START = const (0x00 )
37
+ _STATE_DHCP_DISCOVER = const (0x01 )
38
+ _STATE_DHCP_REQUEST = const (0x02 )
39
+ _STATE_DHCP_LEASED = const (0x03 )
40
+ _STATE_DHCP_REREQUEST = const (0x04 )
41
+ _STATE_DHCP_RELEASE = const (0x05 )
42
+ _STATE_DHCP_WAIT = const (0x06 )
43
+ _STATE_DHCP_DISCONN = const (0x07 )
44
44
45
45
# DHCP wait time between attempts
46
- DHCP_WAIT_TIME = const (60 )
46
+ _DHCP_WAIT_TIME = const (60 )
47
47
48
48
# DHCP Message Types
49
- DHCP_DISCOVER = const (1 )
50
- DHCP_OFFER = const (2 )
51
- DHCP_REQUEST = const (3 )
52
- DHCP_DECLINE = const (4 )
53
- DHCP_ACK = const (5 )
54
- DHCP_NAK = const (6 )
55
- DHCP_RELEASE = const (7 )
56
- DHCP_INFORM = const (8 )
49
+ _DHCP_DISCOVER = const (1 )
50
+ _DHCP_OFFER = const (2 )
51
+ _DHCP_REQUEST = const (3 )
52
+ _DHCP_DECLINE = const (4 )
53
+ _DHCP_ACK = const (5 )
54
+ _DHCP_NAK = const (6 )
55
+ _DHCP_RELEASE = const (7 )
56
+ _DHCP_INFORM = const (8 )
57
57
58
58
# DHCP Message OP Codes
59
- DHCP_BOOT_REQUEST = const (0x01 )
60
- DHCP_BOOT_REPLY = const (0x02 )
59
+ _DHCP_BOOT_REQUEST = const (0x01 )
60
+ _DHCP_BOOT_REPLY = const (0x02 )
61
61
62
- DHCP_HTYPE10MB = const (0x01 )
63
- DHCP_HTYPE100MB = const (0x02 )
62
+ _DHCP_HTYPE10MB = const (0x01 )
63
+ _DHCP_HTYPE100MB = const (0x02 )
64
64
65
- DHCP_HLENETHERNET = const (0x06 )
66
- DHCP_HOPS = const (0x00 )
65
+ _DHCP_HLENETHERNET = const (0x06 )
66
+ _DHCP_HOPS = const (0x00 )
67
67
68
- MAGIC_COOKIE = const (0x63825363 )
69
- MAX_DHCP_OPT = const (0x10 )
68
+ _MAGIC_COOKIE = const (0x63825363 )
69
+ _MAX_DHCP_OPT = const (0x10 )
70
70
71
71
# Default DHCP Server port
72
- DHCP_SERVER_PORT = const (67 )
72
+ _DHCP_SERVER_PORT = const (67 )
73
73
# DHCP Lease Time, in seconds
74
- DEFAULT_LEASE_TIME = const (900 )
75
- BROADCAST_SERVER_ADDR = (255 , 255 , 255 , 255 )
74
+ _DEFAULT_LEASE_TIME = const (900 )
75
+ _BROADCAST_SERVER_ADDR = (255 , 255 , 255 , 255 )
76
76
77
77
# DHCP Response Options
78
- MSG_TYPE = 53
79
- SUBNET_MASK = 1
80
- ROUTERS_ON_SUBNET = 3
81
- DNS_SERVERS = 6
82
- DHCP_SERVER_ID = 54
83
- T1_VAL = 58
84
- T2_VAL = 59
85
- LEASE_TIME = 51
86
- OPT_END = 255
78
+ _MSG_TYPE = 53
79
+ _SUBNET_MASK = 1
80
+ _ROUTERS_ON_SUBNET = 3
81
+ _DNS_SERVERS = 6
82
+ _DHCP_SERVER_ID = 54
83
+ _T1_VAL = 58
84
+ _T2_VAL = 59
85
+ _LEASE_TIME = 51
86
+ _OPT_END = 255
87
87
88
88
# Packet buffer
89
89
_BUFF = bytearray (318 )
@@ -123,13 +123,13 @@ def __init__(
123
123
self ._sock = None
124
124
125
125
# DHCP state machine
126
- self ._dhcp_state = STATE_DHCP_START
126
+ self ._dhcp_state = _STATE_DHCP_START
127
127
self ._initial_xid = 0
128
128
self ._transaction_id = 0
129
129
self ._start_time = 0
130
130
131
131
# DHCP server configuration
132
- self .dhcp_server_ip = BROADCAST_SERVER_ADDR
132
+ self .dhcp_server_ip = _BROADCAST_SERVER_ADDR
133
133
self .local_ip = 0
134
134
self .gateway_ip = 0
135
135
self .subnet_mask = 0
@@ -168,13 +168,13 @@ def send_dhcp_message(
168
168
"""
169
169
_BUFF [:] = b"\x00 " * len (_BUFF )
170
170
# OP
171
- _BUFF [0 ] = DHCP_BOOT_REQUEST
171
+ _BUFF [0 ] = _DHCP_BOOT_REQUEST
172
172
# HTYPE
173
- _BUFF [1 ] = DHCP_HTYPE10MB
173
+ _BUFF [1 ] = _DHCP_HTYPE10MB
174
174
# HLEN
175
- _BUFF [2 ] = DHCP_HLENETHERNET
175
+ _BUFF [2 ] = _DHCP_HLENETHERNET
176
176
# HOPS
177
- _BUFF [3 ] = DHCP_HOPS
177
+ _BUFF [3 ] = _DHCP_HOPS
178
178
179
179
# Transaction ID (xid)
180
180
self ._initial_xid = htonl (self ._transaction_id )
@@ -203,10 +203,10 @@ def send_dhcp_message(
203
203
# NOTE: 192 octets of 0's, BOOTP legacy
204
204
205
205
# Magic Cookie
206
- _BUFF [236 ] = (MAGIC_COOKIE >> 24 ) & 0xFF
207
- _BUFF [237 ] = (MAGIC_COOKIE >> 16 ) & 0xFF
208
- _BUFF [238 ] = (MAGIC_COOKIE >> 8 ) & 0xFF
209
- _BUFF [239 ] = MAGIC_COOKIE & 0xFF
206
+ _BUFF [236 ] = (_MAGIC_COOKIE >> 24 ) & 0xFF
207
+ _BUFF [237 ] = (_MAGIC_COOKIE >> 16 ) & 0xFF
208
+ _BUFF [238 ] = (_MAGIC_COOKIE >> 8 ) & 0xFF
209
+ _BUFF [239 ] = _MAGIC_COOKIE & 0xFF
210
210
211
211
# Option - DHCP Message Type
212
212
_BUFF [240 ] = 53
@@ -230,7 +230,7 @@ def send_dhcp_message(
230
230
_BUFF [253 ] = hostname_len
231
231
_BUFF [254 :after_hostname ] = self ._hostname
232
232
233
- if state == DHCP_REQUEST and not renew :
233
+ if state == _DHCP_REQUEST and not renew :
234
234
# Set the parsed local IP addr
235
235
_BUFF [after_hostname ] = 50
236
236
_BUFF [after_hostname + 1 ] = 0x04
@@ -275,7 +275,7 @@ def parse_dhcp_response(
275
275
# -- Parse Packet, FIXED -- #
276
276
# Validate OP
277
277
assert (
278
- _BUFF [0 ] == DHCP_BOOT_REPLY
278
+ _BUFF [0 ] == _DHCP_BOOT_REPLY
279
279
), "Malformed Packet - \
280
280
DHCP message OP is not expected BOOT Reply."
281
281
@@ -288,55 +288,55 @@ def parse_dhcp_response(
288
288
if _BUFF [28 :34 ] == 0 :
289
289
return 0 , 0
290
290
291
- if int .from_bytes (_BUFF [235 :240 ], "big" ) != MAGIC_COOKIE :
291
+ if int .from_bytes (_BUFF [235 :240 ], "big" ) != _MAGIC_COOKIE :
292
292
return 0 , 0
293
293
294
294
# -- Parse Packet, VARIABLE -- #
295
295
ptr = 240
296
- while _BUFF [ptr ] != OPT_END :
297
- if _BUFF [ptr ] == MSG_TYPE :
296
+ while _BUFF [ptr ] != _OPT_END :
297
+ if _BUFF [ptr ] == _MSG_TYPE :
298
298
ptr += 1
299
299
opt_len = _BUFF [ptr ]
300
300
ptr += opt_len
301
301
msg_type = _BUFF [ptr ]
302
302
ptr += 1
303
- elif _BUFF [ptr ] == SUBNET_MASK :
303
+ elif _BUFF [ptr ] == _SUBNET_MASK :
304
304
ptr += 1
305
305
opt_len = _BUFF [ptr ]
306
306
ptr += 1
307
307
self .subnet_mask = tuple (_BUFF [ptr : ptr + opt_len ])
308
308
ptr += opt_len
309
- elif _BUFF [ptr ] == DHCP_SERVER_ID :
309
+ elif _BUFF [ptr ] == _DHCP_SERVER_ID :
310
310
ptr += 1
311
311
opt_len = _BUFF [ptr ]
312
312
ptr += 1
313
313
self .dhcp_server_ip = tuple (_BUFF [ptr : ptr + opt_len ])
314
314
ptr += opt_len
315
- elif _BUFF [ptr ] == LEASE_TIME :
315
+ elif _BUFF [ptr ] == _LEASE_TIME :
316
316
ptr += 1
317
317
opt_len = _BUFF [ptr ]
318
318
ptr += 1
319
319
self ._lease_time = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "big" )
320
320
ptr += opt_len
321
- elif _BUFF [ptr ] == ROUTERS_ON_SUBNET :
321
+ elif _BUFF [ptr ] == _ROUTERS_ON_SUBNET :
322
322
ptr += 1
323
323
opt_len = _BUFF [ptr ]
324
324
ptr += 1
325
325
self .gateway_ip = tuple (_BUFF [ptr : ptr + opt_len ])
326
326
ptr += opt_len
327
- elif _BUFF [ptr ] == DNS_SERVERS :
327
+ elif _BUFF [ptr ] == _DNS_SERVERS :
328
328
ptr += 1
329
329
opt_len = _BUFF [ptr ]
330
330
ptr += 1
331
331
self .dns_server_ip = tuple (_BUFF [ptr : ptr + 4 ])
332
332
ptr += opt_len # still increment even though we only read 1 addr.
333
- elif _BUFF [ptr ] == T1_VAL :
333
+ elif _BUFF [ptr ] == _T1_VAL :
334
334
ptr += 1
335
335
opt_len = _BUFF [ptr ]
336
336
ptr += 1
337
337
self ._t1 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "big" )
338
338
ptr += opt_len
339
- elif _BUFF [ptr ] == T2_VAL :
339
+ elif _BUFF [ptr ] == _T2_VAL :
340
340
ptr += 1
341
341
opt_len = _BUFF [ptr ]
342
342
ptr += 1
@@ -379,55 +379,55 @@ def _dhcp_state_machine(self) -> None:
379
379
the non-blocking DHCP maintenance function.
380
380
"""
381
381
if self ._eth .link_status :
382
- if self ._dhcp_state == STATE_DHCP_DISCONN :
383
- self ._dhcp_state = STATE_DHCP_START
382
+ if self ._dhcp_state == _STATE_DHCP_DISCONN :
383
+ self ._dhcp_state = _STATE_DHCP_START
384
384
else :
385
- if self ._dhcp_state != STATE_DHCP_DISCONN :
386
- self ._dhcp_state = STATE_DHCP_DISCONN
387
- self .dhcp_server_ip = BROADCAST_SERVER_ADDR
385
+ if self ._dhcp_state != _STATE_DHCP_DISCONN :
386
+ self ._dhcp_state = _STATE_DHCP_DISCONN
387
+ self .dhcp_server_ip = _BROADCAST_SERVER_ADDR
388
388
self ._last_lease_time = 0
389
389
reset_ip = (0 , 0 , 0 , 0 )
390
390
self ._eth .ifconfig = (reset_ip , reset_ip , reset_ip , reset_ip )
391
391
if self ._sock is not None :
392
392
self ._sock .close ()
393
393
self ._sock = None
394
394
395
- if self ._dhcp_state == STATE_DHCP_START :
395
+ if self ._dhcp_state == _STATE_DHCP_START :
396
396
self ._start_time = time .monotonic ()
397
397
self ._transaction_id = (self ._transaction_id + 1 ) & 0x7FFFFFFF
398
398
try :
399
399
self ._sock = socket .socket (type = socket .SOCK_DGRAM )
400
400
except RuntimeError :
401
401
if self ._debug :
402
402
print ("* DHCP: Failed to allocate socket" )
403
- self ._dhcp_state = STATE_DHCP_WAIT
403
+ self ._dhcp_state = _STATE_DHCP_WAIT
404
404
else :
405
405
self ._sock .settimeout (self ._response_timeout )
406
406
self ._sock .bind ((None , 68 ))
407
- self ._sock .connect ((self .dhcp_server_ip , DHCP_SERVER_PORT ))
407
+ self ._sock .connect ((self .dhcp_server_ip , _DHCP_SERVER_PORT ))
408
408
if self ._last_lease_time == 0 or time .monotonic () > (
409
409
self ._last_lease_time + self ._lease_time
410
410
):
411
411
if self ._debug :
412
412
print ("* DHCP: Send discover to {}" .format (self .dhcp_server_ip ))
413
413
self .send_dhcp_message (
414
- STATE_DHCP_DISCOVER , (time .monotonic () - self ._start_time )
414
+ _STATE_DHCP_DISCOVER , (time .monotonic () - self ._start_time )
415
415
)
416
- self ._dhcp_state = STATE_DHCP_DISCOVER
416
+ self ._dhcp_state = _STATE_DHCP_DISCOVER
417
417
else :
418
418
if self ._debug :
419
419
print ("* DHCP: Send request to {}" .format (self .dhcp_server_ip ))
420
420
self .send_dhcp_message (
421
- DHCP_REQUEST , (time .monotonic () - self ._start_time ), True
421
+ _DHCP_REQUEST , (time .monotonic () - self ._start_time ), True
422
422
)
423
- self ._dhcp_state = STATE_DHCP_REQUEST
423
+ self ._dhcp_state = _STATE_DHCP_REQUEST
424
424
425
- elif self ._dhcp_state == STATE_DHCP_DISCOVER :
425
+ elif self ._dhcp_state == _STATE_DHCP_DISCOVER :
426
426
if self ._sock .available ():
427
427
if self ._debug :
428
428
print ("* DHCP: Parsing OFFER" )
429
429
msg_type , xid = self .parse_dhcp_response ()
430
- if msg_type == DHCP_OFFER :
430
+ if msg_type == _DHCP_OFFER :
431
431
# Check if transaction ID matches, otherwise it may be an offer
432
432
# for another device
433
433
if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
@@ -437,33 +437,33 @@ def _dhcp_state_machine(self) -> None:
437
437
)
438
438
self ._transaction_id = (self ._transaction_id + 1 ) & 0x7FFFFFFF
439
439
self .send_dhcp_message (
440
- DHCP_REQUEST , (time .monotonic () - self ._start_time )
440
+ _DHCP_REQUEST , (time .monotonic () - self ._start_time )
441
441
)
442
- self ._dhcp_state = STATE_DHCP_REQUEST
442
+ self ._dhcp_state = _STATE_DHCP_REQUEST
443
443
else :
444
444
if self ._debug :
445
445
print ("* DHCP: Received OFFER with non-matching xid" )
446
446
else :
447
447
if self ._debug :
448
448
print ("* DHCP: Received DHCP Message is not OFFER" )
449
449
450
- elif self ._dhcp_state == STATE_DHCP_REQUEST :
450
+ elif self ._dhcp_state == _STATE_DHCP_REQUEST :
451
451
if self ._sock .available ():
452
452
if self ._debug :
453
453
print ("* DHCP: Parsing ACK" )
454
454
msg_type , xid = self .parse_dhcp_response ()
455
455
# Check if transaction ID matches, otherwise it may be
456
456
# for another device
457
457
if htonl (self ._transaction_id ) == int .from_bytes (xid , "big" ):
458
- if msg_type == DHCP_ACK :
458
+ if msg_type == _DHCP_ACK :
459
459
if self ._debug :
460
460
print ("* DHCP: Successful lease" )
461
461
self ._sock .close ()
462
462
self ._sock = None
463
- self ._dhcp_state = STATE_DHCP_LEASED
463
+ self ._dhcp_state = _STATE_DHCP_LEASED
464
464
self ._last_lease_time = self ._start_time
465
465
if self ._lease_time == 0 :
466
- self ._lease_time = DEFAULT_LEASE_TIME
466
+ self ._lease_time = _DEFAULT_LEASE_TIME
467
467
if self ._t1 == 0 :
468
468
# T1 is 50% of _lease_time
469
469
self ._t1 = self ._lease_time >> 1
@@ -486,41 +486,41 @@ def _dhcp_state_machine(self) -> None:
486
486
if self ._debug :
487
487
print ("* DHCP: Received non-matching xid" )
488
488
489
- elif self ._dhcp_state == STATE_DHCP_WAIT :
490
- if time .monotonic () > (self ._start_time + DHCP_WAIT_TIME ):
489
+ elif self ._dhcp_state == _STATE_DHCP_WAIT :
490
+ if time .monotonic () > (self ._start_time + _DHCP_WAIT_TIME ):
491
491
if self ._debug :
492
492
print ("* DHCP: Begin retry" )
493
- self ._dhcp_state = STATE_DHCP_START
493
+ self ._dhcp_state = _STATE_DHCP_START
494
494
if time .monotonic () > (self ._last_lease_time + self ._rebind_in_sec ):
495
- self .dhcp_server_ip = BROADCAST_SERVER_ADDR
495
+ self .dhcp_server_ip = _BROADCAST_SERVER_ADDR
496
496
if time .monotonic () > (self ._last_lease_time + self ._lease_time ):
497
497
reset_ip = (0 , 0 , 0 , 0 )
498
498
self ._eth .ifconfig = (reset_ip , reset_ip , reset_ip , reset_ip )
499
499
500
- elif self ._dhcp_state == STATE_DHCP_LEASED :
500
+ elif self ._dhcp_state == _STATE_DHCP_LEASED :
501
501
if time .monotonic () > (self ._last_lease_time + self ._renew_in_sec ):
502
- self ._dhcp_state = STATE_DHCP_START
502
+ self ._dhcp_state = _STATE_DHCP_START
503
503
if self ._debug :
504
504
print ("* DHCP: Time to renew lease" )
505
505
506
506
if self ._dhcp_state in (
507
- STATE_DHCP_DISCOVER ,
508
- STATE_DHCP_REQUEST ,
507
+ _STATE_DHCP_DISCOVER ,
508
+ _STATE_DHCP_REQUEST ,
509
509
) and time .monotonic () > (self ._start_time + self ._response_timeout ):
510
- self ._dhcp_state = STATE_DHCP_WAIT
510
+ self ._dhcp_state = _STATE_DHCP_WAIT
511
511
if self ._sock is not None :
512
512
self ._sock .close ()
513
513
self ._sock = None
514
514
515
515
def request_dhcp_lease (self ) -> bool :
516
516
"""Request to renew or acquire a DHCP lease."""
517
- if self ._dhcp_state in (STATE_DHCP_LEASED , STATE_DHCP_WAIT ):
518
- self ._dhcp_state = STATE_DHCP_START
517
+ if self ._dhcp_state in (_STATE_DHCP_LEASED , _STATE_DHCP_WAIT ):
518
+ self ._dhcp_state = _STATE_DHCP_START
519
519
520
- while self ._dhcp_state not in (STATE_DHCP_LEASED , STATE_DHCP_WAIT ):
520
+ while self ._dhcp_state not in (_STATE_DHCP_LEASED , _STATE_DHCP_WAIT ):
521
521
self ._dhcp_state_machine ()
522
522
523
- return self ._dhcp_state == STATE_DHCP_LEASED
523
+ return self ._dhcp_state == _STATE_DHCP_LEASED
524
524
525
525
def maintain_dhcp_lease (self ) -> None :
526
526
"""Maintain DHCP lease"""
0 commit comments