3939# pylint: disable=bad-whitespace
4040
4141# 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 )
4646STATE_DHCP_REREQUEST = const (0x04 )
47- STATE_DHCP_RELEASE = const (0x05 )
47+ STATE_DHCP_RELEASE = const (0x05 )
4848
4949# DHCP Message Types
5050DHCP_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 )
5858
5959# DHCP Message OP Codes
6060DHCP_BOOT_REQUEST = const (0x01 )
61- DHCP_BOOT_REPLY = const (0x02 )
61+ DHCP_BOOT_REPLY = const (0x02 )
6262
63- DHCP_HTYPE10MB = const (0x01 )
63+ DHCP_HTYPE10MB = const (0x01 )
6464DHCP_HTYPE100MB = const (0x02 )
6565
6666DHCP_HLENETHERNET = const (0x06 )
67- DHCP_HOPS = const (0x00 )
67+ DHCP_HOPS = const (0x00 )
6868
6969MAGIC_COOKIE = const (0x63825363 )
7070MAX_DHCP_OPT = const (0x10 )
7171
7272# Default DHCP Server port
73- DHCP_SERVER_PORT = const (67 )
73+ DHCP_SERVER_PORT = const (67 )
7474# DHCP Lease Time, in seconds
7575DEFAULT_LEASE_TIME = const (900 )
76- BROADCAST_SERVER_ADDR = ' 255.255.255.255'
76+ BROADCAST_SERVER_ADDR = " 255.255.255.255"
7777
7878# DHCP Response Options
79- MSG_TYPE = 53
80- SUBNET_MASK = 1
79+ MSG_TYPE = 53
80+ SUBNET_MASK = 1
8181ROUTERS_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
8888
8989
9090# pylint: enable=bad-whitespace
9191_BUFF = bytearray (317 )
9292
93+
9394class DHCP :
9495 """W5k DHCP Client implementation.
9596 :param eth: Wiznet 5k object
@@ -148,16 +149,16 @@ def send_dhcp_message(self, state, time_elapsed):
148149
149150 # Transaction ID (xid)
150151 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" )
152153 _BUFF [4 :7 ] = self ._initial_xid
153154
154155 # 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
157158
158159 # flags
159160 flags = htons (0x8000 )
160- flags = flags .to_bytes (2 , 'b' )
161+ flags = flags .to_bytes (2 , "b" )
161162 _BUFF [10 ] = flags [1 ]
162163 _BUFF [11 ] = flags [0 ]
163164
@@ -170,10 +171,10 @@ def send_dhcp_message(self, state, time_elapsed):
170171 # NOTE: 192 octets of 0's, BOOTP legacy
171172
172173 # 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
177178
178179 # Option - DHCP Message Type
179180 _BUFF [240 ] = 53
@@ -188,15 +189,15 @@ def send_dhcp_message(self, state, time_elapsed):
188189 _BUFF [245 ] = 0x01
189190 # Client MAC Address
190191 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 ]
192193
193194 # Option - Host Name
194195 _BUFF [252 ] = 12
195196 _BUFF [253 ] = len (b"Wiznet" ) + 6
196197 _BUFF [254 :260 ] = b"WIZnet"
197198
198199 for mac in range (0 , 5 ):
199- _BUFF [260 + mac ] = self ._mac_address [mac ]
200+ _BUFF [260 + mac ] = self ._mac_address [mac ]
200201
201202 if state == DHCP_REQUEST :
202203 # Set the parsed local IP addr
@@ -228,7 +229,9 @@ def send_dhcp_message(self, state, time_elapsed):
228229 # Send DHCP packet
229230 self ._sock .send (_BUFF )
230231
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
232235 """Parse DHCP response from DHCP server.
233236 Returns DHCP packet type.
234237
@@ -248,7 +251,9 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
248251
249252 # -- Parse Packet, FIXED -- #
250253 # Validate OP
251- assert _BUFF [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
254+ assert (
255+ _BUFF [0 ] == DHCP_BOOT_REPLY
256+ ), "Malformed Packet - \
252257 DHCP message OP is not expected BOOT Reply."
253258
254259 xid = _BUFF [4 :8 ]
@@ -260,7 +265,7 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
260265 if _BUFF [28 :34 ] == 0 :
261266 return 0 , 0
262267
263- if int .from_bytes (_BUFF [235 :240 ], 'l' ) != MAGIC_COOKIE :
268+ if int .from_bytes (_BUFF [235 :240 ], "l" ) != MAGIC_COOKIE :
264269 return 0 , 0
265270
266271 # -- Parse Packet, VARIABLE -- #
@@ -276,43 +281,43 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
276281 ptr += 1
277282 opt_len = _BUFF [ptr ]
278283 ptr += 1
279- self .subnet_mask = _BUFF [ptr : ptr + opt_len ]
284+ self .subnet_mask = _BUFF [ptr : ptr + opt_len ]
280285 ptr += opt_len
281286 elif _BUFF [ptr ] == DHCP_SERVER_ID :
282287 ptr += 1
283288 opt_len = _BUFF [ptr ]
284289 ptr += 1
285- self .dhcp_server_ip = _BUFF [ptr : ptr + opt_len ]
290+ self .dhcp_server_ip = _BUFF [ptr : ptr + opt_len ]
286291 ptr += opt_len
287292 elif _BUFF [ptr ] == LEASE_TIME :
288293 ptr += 1
289294 opt_len = _BUFF [ptr ]
290295 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" )
292297 ptr += opt_len
293298 elif _BUFF [ptr ] == ROUTERS_ON_SUBNET :
294299 ptr += 1
295300 opt_len = _BUFF [ptr ]
296301 ptr += 1
297- self .gateway_ip = _BUFF [ptr : ptr + opt_len ]
302+ self .gateway_ip = _BUFF [ptr : ptr + opt_len ]
298303 ptr += opt_len
299304 elif _BUFF [ptr ] == DNS_SERVERS :
300305 ptr += 1
301306 opt_len = _BUFF [ptr ]
302307 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.
305310 elif _BUFF [ptr ] == T1_VAL :
306311 ptr += 1
307312 opt_len = _BUFF [ptr ]
308313 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" )
310315 ptr += opt_len
311316 elif _BUFF [ptr ] == T2_VAL :
312317 ptr += 1
313318 opt_len = _BUFF [ptr ]
314319 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" )
316321 ptr += opt_len
317322 elif _BUFF [ptr ] == 0 :
318323 break
@@ -325,18 +330,26 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
325330 ptr += opt_len
326331
327332 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+ )
335346
336347 gc .collect ()
337348 return msg_type , xid
338349
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
340353 """Request to renew or acquire a DHCP lease.
341354
342355 """
@@ -353,8 +366,9 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
353366 self ._sock .connect (((BROADCAST_SERVER_ADDR ), DHCP_SERVER_PORT ))
354367 if self ._debug :
355368 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+ )
358372 self ._dhcp_state = STATE_DHCP_DISCOVER
359373 elif self ._dhcp_state == STATE_DHCP_DISCOVER :
360374 if self ._debug :
@@ -363,10 +377,12 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
363377 if msg_type == DHCP_OFFER :
364378 # use the _transaction_id the offer returned,
365379 # 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" )
367381 if self ._debug :
368382 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+ )
370386 self ._dhcp_state = STATE_DHCP_REQUEST
371387 else :
372388 print ("* Received DHCP Message is not OFFER" )
@@ -396,7 +412,7 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-stat
396412 msg_type = 0
397413 self ._dhcp_state = STATE_DHCP_START
398414
399- if ( result != 1 and ((time .monotonic () - start_time > self ._timeout ) )):
415+ if result != 1 and ((time .monotonic () - start_time > self ._timeout )):
400416 break
401417
402418 self ._transaction_id += 1
0 commit comments