39
39
# pylint: disable=bad-whitespace
40
40
41
41
# DHCP State Machine
42
- STATE_DHCP_START = const (0x00 )
43
- STATE_DHCP_DISCOVER = const (0x01 )
44
- STATE_DHCP_REQUEST = const (0x02 )
45
- STATE_DHCP_LEASED = const (0x03 )
42
+ STATE_DHCP_START = const (0x00 )
43
+ STATE_DHCP_DISCOVER = const (0x01 )
44
+ STATE_DHCP_REQUEST = const (0x02 )
45
+ STATE_DHCP_LEASED = const (0x03 )
46
46
STATE_DHCP_REREQUEST = const (0x04 )
47
- STATE_DHCP_RELEASE = const (0x05 )
47
+ STATE_DHCP_RELEASE = const (0x05 )
48
48
49
49
# DHCP Message Types
50
50
DHCP_DISCOVER = const (1 )
51
- DHCP_OFFER = const (2 )
52
- DHCP_REQUEST = const (3 )
53
- DHCP_DECLINE = const (4 )
54
- DHCP_ACK = const (5 )
55
- DHCP_NAK = const (6 )
56
- DHCP_RELEASE = const (7 )
57
- DHCP_INFORM = const (8 )
51
+ DHCP_OFFER = const (2 )
52
+ DHCP_REQUEST = const (3 )
53
+ DHCP_DECLINE = const (4 )
54
+ DHCP_ACK = const (5 )
55
+ DHCP_NAK = const (6 )
56
+ DHCP_RELEASE = const (7 )
57
+ DHCP_INFORM = const (8 )
58
58
59
59
# DHCP Message OP Codes
60
60
DHCP_BOOT_REQUEST = const (0x01 )
61
- DHCP_BOOT_REPLY = const (0x02 )
61
+ DHCP_BOOT_REPLY = const (0x02 )
62
62
63
- DHCP_HTYPE10MB = const (0x01 )
63
+ DHCP_HTYPE10MB = const (0x01 )
64
64
DHCP_HTYPE100MB = const (0x02 )
65
65
66
66
DHCP_HLENETHERNET = const (0x06 )
67
- DHCP_HOPS = const (0x00 )
67
+ DHCP_HOPS = const (0x00 )
68
68
69
69
MAGIC_COOKIE = const (0x63825363 )
70
70
MAX_DHCP_OPT = const (0x10 )
71
71
72
72
# Default DHCP Server port
73
- DHCP_SERVER_PORT = const (67 )
73
+ DHCP_SERVER_PORT = const (67 )
74
74
# DHCP Lease Time, in seconds
75
75
DEFAULT_LEASE_TIME = const (900 )
76
- BROADCAST_SERVER_ADDR = ' 255.255.255.255'
76
+ BROADCAST_SERVER_ADDR = " 255.255.255.255"
77
77
78
78
# DHCP Response Options
79
- MSG_TYPE = 53
80
- SUBNET_MASK = 1
79
+ MSG_TYPE = 53
80
+ SUBNET_MASK = 1
81
81
ROUTERS_ON_SUBNET = 3
82
- DNS_SERVERS = 6
83
- DHCP_SERVER_ID = 54
84
- T1_VAL = 58
85
- T2_VAL = 59
86
- LEASE_TIME = 51
87
- OPT_END = 255
82
+ DNS_SERVERS = 6
83
+ DHCP_SERVER_ID = 54
84
+ T1_VAL = 58
85
+ T2_VAL = 59
86
+ LEASE_TIME = 51
87
+ OPT_END = 255
88
88
89
89
90
90
# pylint: enable=bad-whitespace
91
91
_BUFF = bytearray (317 )
92
92
93
+
93
94
class DHCP :
94
95
"""W5k DHCP Client implementation.
95
96
:param eth: Wiznet 5k object
@@ -148,16 +149,16 @@ def send_dhcp_message(self, state, time_elapsed):
148
149
149
150
# Transaction ID (xid)
150
151
self ._initial_xid = htonl (self ._transaction_id )
151
- self ._initial_xid = self ._initial_xid .to_bytes (4 , 'l' )
152
+ self ._initial_xid = self ._initial_xid .to_bytes (4 , "l" )
152
153
_BUFF [4 :7 ] = self ._initial_xid
153
154
154
155
# seconds elapsed
155
- _BUFF [8 ] = (( int (time_elapsed ) & 0xff00 ) >> 8 )
156
- _BUFF [9 ] = ( int (time_elapsed ) & 0x00ff )
156
+ _BUFF [8 ] = (int (time_elapsed ) & 0xFF00 ) >> 8
157
+ _BUFF [9 ] = int (time_elapsed ) & 0x00FF
157
158
158
159
# flags
159
160
flags = htons (0x8000 )
160
- flags = flags .to_bytes (2 , 'b' )
161
+ flags = flags .to_bytes (2 , "b" )
161
162
_BUFF [10 ] = flags [1 ]
162
163
_BUFF [11 ] = flags [0 ]
163
164
@@ -170,10 +171,10 @@ def send_dhcp_message(self, state, time_elapsed):
170
171
# NOTE: 192 octets of 0's, BOOTP legacy
171
172
172
173
# Magic Cookie
173
- _BUFF [236 ] = (( MAGIC_COOKIE >> 24 )& 0xFF )
174
- _BUFF [237 ] = (( MAGIC_COOKIE >> 16 )& 0xFF )
175
- _BUFF [238 ] = (( MAGIC_COOKIE >> 8 )& 0xFF )
176
- _BUFF [239 ] = ( MAGIC_COOKIE & 0xFF )
174
+ _BUFF [236 ] = (MAGIC_COOKIE >> 24 ) & 0xFF
175
+ _BUFF [237 ] = (MAGIC_COOKIE >> 16 ) & 0xFF
176
+ _BUFF [238 ] = (MAGIC_COOKIE >> 8 ) & 0xFF
177
+ _BUFF [239 ] = MAGIC_COOKIE & 0xFF
177
178
178
179
# Option - DHCP Message Type
179
180
_BUFF [240 ] = 53
@@ -188,15 +189,15 @@ def send_dhcp_message(self, state, time_elapsed):
188
189
_BUFF [245 ] = 0x01
189
190
# Client MAC Address
190
191
for mac in range (0 , len (self ._mac_address )):
191
- _BUFF [246 + mac ] = self ._mac_address [mac ]
192
+ _BUFF [246 + mac ] = self ._mac_address [mac ]
192
193
193
194
# Option - Host Name
194
195
_BUFF [252 ] = 12
195
196
_BUFF [253 ] = len (b"Wiznet" ) + 6
196
197
_BUFF [254 :260 ] = b"WIZnet"
197
198
198
199
for mac in range (0 , 5 ):
199
- _BUFF [260 + mac ] = self ._mac_address [mac ]
200
+ _BUFF [260 + mac ] = self ._mac_address [mac ]
200
201
201
202
if state == DHCP_REQUEST :
202
203
# Set the parsed local IP addr
@@ -228,7 +229,9 @@ def send_dhcp_message(self, state, time_elapsed):
228
229
# Send DHCP packet
229
230
self ._sock .send (_BUFF )
230
231
231
- def parse_dhcp_response (self , response_timeout ): # pylint: disable=too-many-branches, too-many-statements
232
+ def parse_dhcp_response (
233
+ self , response_timeout
234
+ ): # pylint: disable=too-many-branches, too-many-statements
232
235
"""Parse DHCP response from DHCP server.
233
236
Returns DHCP packet type.
234
237
@@ -248,7 +251,9 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
248
251
249
252
# -- Parse Packet, FIXED -- #
250
253
# Validate OP
251
- assert _BUFF [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
254
+ assert (
255
+ _BUFF [0 ] == DHCP_BOOT_REPLY
256
+ ), "Malformed Packet - \
252
257
DHCP message OP is not expected BOOT Reply."
253
258
254
259
xid = _BUFF [4 :8 ]
@@ -260,7 +265,7 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
260
265
if _BUFF [28 :34 ] == 0 :
261
266
return 0 , 0
262
267
263
- if int .from_bytes (_BUFF [235 :240 ], 'l' ) != MAGIC_COOKIE :
268
+ if int .from_bytes (_BUFF [235 :240 ], "l" ) != MAGIC_COOKIE :
264
269
return 0 , 0
265
270
266
271
# -- Parse Packet, VARIABLE -- #
@@ -276,43 +281,43 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
276
281
ptr += 1
277
282
opt_len = _BUFF [ptr ]
278
283
ptr += 1
279
- self .subnet_mask = _BUFF [ptr : ptr + opt_len ]
284
+ self .subnet_mask = _BUFF [ptr : ptr + opt_len ]
280
285
ptr += opt_len
281
286
elif _BUFF [ptr ] == DHCP_SERVER_ID :
282
287
ptr += 1
283
288
opt_len = _BUFF [ptr ]
284
289
ptr += 1
285
- self .dhcp_server_ip = _BUFF [ptr : ptr + opt_len ]
290
+ self .dhcp_server_ip = _BUFF [ptr : ptr + opt_len ]
286
291
ptr += opt_len
287
292
elif _BUFF [ptr ] == LEASE_TIME :
288
293
ptr += 1
289
294
opt_len = _BUFF [ptr ]
290
295
ptr += 1
291
- self ._lease_time = int .from_bytes (_BUFF [ptr : ptr + opt_len ], 'l' )
296
+ self ._lease_time = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "l" )
292
297
ptr += opt_len
293
298
elif _BUFF [ptr ] == ROUTERS_ON_SUBNET :
294
299
ptr += 1
295
300
opt_len = _BUFF [ptr ]
296
301
ptr += 1
297
- self .gateway_ip = _BUFF [ptr : ptr + opt_len ]
302
+ self .gateway_ip = _BUFF [ptr : ptr + opt_len ]
298
303
ptr += opt_len
299
304
elif _BUFF [ptr ] == DNS_SERVERS :
300
305
ptr += 1
301
306
opt_len = _BUFF [ptr ]
302
307
ptr += 1
303
- self .dns_server_ip = _BUFF [ptr : ptr + 4 ]
304
- ptr += opt_len # still increment even though we only read 1 addr.
308
+ self .dns_server_ip = _BUFF [ptr : ptr + 4 ]
309
+ ptr += opt_len # still increment even though we only read 1 addr.
305
310
elif _BUFF [ptr ] == T1_VAL :
306
311
ptr += 1
307
312
opt_len = _BUFF [ptr ]
308
313
ptr += 1
309
- self ._t1 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], 'l' )
314
+ self ._t1 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "l" )
310
315
ptr += opt_len
311
316
elif _BUFF [ptr ] == T2_VAL :
312
317
ptr += 1
313
318
opt_len = _BUFF [ptr ]
314
319
ptr += 1
315
- self ._t2 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], 'l' )
320
+ self ._t2 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "l" )
316
321
ptr += opt_len
317
322
elif _BUFF [ptr ] == 0 :
318
323
break
@@ -325,18 +330,26 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
325
330
ptr += opt_len
326
331
327
332
if self ._debug :
328
- print ("Msg Type: {}\n Subnet Mask: {}\n DHCP Server ID:{}\n DNS Server IP:{}\
329
- \n Gateway IP:{}\n T1:{}\n T2:{}\n Lease Time:{}" .format (msg_type , self .subnet_mask ,
330
- self .dhcp_server_ip ,
331
- self .dns_server_ip ,
332
- self .gateway_ip ,
333
- self ._t1 , self ._t2 ,
334
- self ._lease_time ))
333
+ print (
334
+ "Msg Type: {}\n Subnet Mask: {}\n DHCP Server ID:{}\n DNS Server IP:{}\
335
+ \n Gateway IP:{}\n T1:{}\n T2:{}\n Lease Time:{}" .format (
336
+ msg_type ,
337
+ self .subnet_mask ,
338
+ self .dhcp_server_ip ,
339
+ self .dns_server_ip ,
340
+ self .gateway_ip ,
341
+ self ._t1 ,
342
+ self ._t2 ,
343
+ self ._lease_time ,
344
+ )
345
+ )
335
346
336
347
gc .collect ()
337
348
return msg_type , xid
338
349
339
- def request_dhcp_lease (self ): # pylint: disable=too-many-branches, too-many-statements
350
+ def request_dhcp_lease (
351
+ self ,
352
+ ): # pylint: disable=too-many-branches, too-many-statements
340
353
"""Request to renew or acquire a DHCP lease.
341
354
342
355
"""
@@ -353,8 +366,9 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
353
366
self ._sock .connect (((BROADCAST_SERVER_ADDR ), DHCP_SERVER_PORT ))
354
367
if self ._debug :
355
368
print ("* DHCP: Discover" )
356
- self .send_dhcp_message (STATE_DHCP_DISCOVER ,
357
- ((time .monotonic () - start_time ) / 1000 ))
369
+ self .send_dhcp_message (
370
+ STATE_DHCP_DISCOVER , ((time .monotonic () - start_time ) / 1000 )
371
+ )
358
372
self ._dhcp_state = STATE_DHCP_DISCOVER
359
373
elif self ._dhcp_state == STATE_DHCP_DISCOVER :
360
374
if self ._debug :
@@ -363,10 +377,12 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
363
377
if msg_type == DHCP_OFFER :
364
378
# use the _transaction_id the offer returned,
365
379
# rather than the current one
366
- self ._transaction_id = self ._transaction_id .from_bytes (xid , 'l' )
380
+ self ._transaction_id = self ._transaction_id .from_bytes (xid , "l" )
367
381
if self ._debug :
368
382
print ("* DHCP: Request" )
369
- self .send_dhcp_message (DHCP_REQUEST , ((time .monotonic () - start_time ) / 1000 ))
383
+ self .send_dhcp_message (
384
+ DHCP_REQUEST , ((time .monotonic () - start_time ) / 1000 )
385
+ )
370
386
self ._dhcp_state = STATE_DHCP_REQUEST
371
387
else :
372
388
print ("* Received DHCP Message is not OFFER" )
@@ -396,7 +412,7 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
396
412
msg_type = 0
397
413
self ._dhcp_state = STATE_DHCP_START
398
414
399
- if ( result != 1 and ((time .monotonic () - start_time > self ._timeout ) )):
415
+ if result != 1 and ((time .monotonic () - start_time > self ._timeout )):
400
416
break
401
417
402
418
self ._transaction_id += 1
0 commit comments