85
85
_LEASE_TIME = 51
86
86
_OPT_END = 255
87
87
88
- # Packet buffer
88
+ # Packet buffer size
89
89
_BUFF_SIZE = const (318 )
90
- _BUFF = bytearray (_BUFF_SIZE )
91
-
92
90
93
91
class DHCP :
94
92
"""W5k DHCP Client implementation."""
@@ -167,95 +165,95 @@ def send_dhcp_message(
167
165
:param float time_elapsed: Number of seconds elapsed since DHCP process started
168
166
:param bool renew: Set True for renew and rebind, defaults to False
169
167
"""
170
- _BUFF = bytearray (_BUFF_SIZE )
168
+ buff = bytearray (_BUFF_SIZE )
171
169
# OP
172
- _BUFF [0 ] = _DHCP_BOOT_REQUEST
170
+ buff [0 ] = _DHCP_BOOT_REQUEST
173
171
# HTYPE
174
- _BUFF [1 ] = _DHCP_HTYPE10MB
172
+ buff [1 ] = _DHCP_HTYPE10MB
175
173
# HLEN
176
- _BUFF [2 ] = _DHCP_HLENETHERNET
174
+ buff [2 ] = _DHCP_HLENETHERNET
177
175
# HOPS
178
- _BUFF [3 ] = _DHCP_HOPS
176
+ buff [3 ] = _DHCP_HOPS
179
177
180
178
# Transaction ID (xid)
181
179
self ._initial_xid = htonl (self ._transaction_id )
182
180
self ._initial_xid = self ._initial_xid .to_bytes (4 , "big" )
183
- _BUFF [4 :8 ] = self ._initial_xid
181
+ buff [4 :8 ] = self ._initial_xid
184
182
185
183
# seconds elapsed
186
- _BUFF [8 ] = (int (time_elapsed ) & 0xFF00 ) >> 8
187
- _BUFF [9 ] = int (time_elapsed ) & 0x00FF
184
+ buff [8 ] = (int (time_elapsed ) & 0xFF00 ) >> 8
185
+ buff [9 ] = int (time_elapsed ) & 0x00FF
188
186
189
187
# flags
190
188
flags = htons (0x8000 )
191
189
flags = flags .to_bytes (2 , "big" )
192
- _BUFF [10 ] = flags [1 ]
193
- _BUFF [11 ] = flags [0 ]
190
+ buff [10 ] = flags [1 ]
191
+ buff [11 ] = flags [0 ]
194
192
195
193
# NOTE: Skipping ciaddr/yiaddr/siaddr/giaddr
196
194
# as they're already set to 0.0.0.0
197
195
# Except when renewing, then fill in ciaddr
198
196
if renew :
199
- _BUFF [12 :16 ] = bytes (self .local_ip )
197
+ buff [12 :16 ] = bytes (self .local_ip )
200
198
201
199
# chaddr
202
- _BUFF [28 :34 ] = self ._mac_address
200
+ buff [28 :34 ] = self ._mac_address
203
201
204
202
# NOTE: 192 octets of 0's, BOOTP legacy
205
203
206
204
# Magic Cookie
207
- _BUFF [236 :240 ] = _MAGIC_COOKIE
205
+ buff [236 :240 ] = _MAGIC_COOKIE
208
206
209
207
# Option - DHCP Message Type
210
- _BUFF [240 ] = 53
211
- _BUFF [241 ] = 0x01
212
- _BUFF [242 ] = state
208
+ buff [240 ] = 53
209
+ buff [241 ] = 0x01
210
+ buff [242 ] = state
213
211
214
212
# Option - Client Identifier
215
- _BUFF [243 ] = 61
213
+ buff [243 ] = 61
216
214
# Length
217
- _BUFF [244 ] = 0x07
215
+ buff [244 ] = 0x07
218
216
# HW Type - ETH
219
- _BUFF [245 ] = 0x01
217
+ buff [245 ] = 0x01
220
218
# Client MAC Address
221
219
for mac , val in enumerate (self ._mac_address ):
222
- _BUFF [246 + mac ] = val
220
+ buff [246 + mac ] = val
223
221
224
222
# Option - Host Name
225
- _BUFF [252 ] = 12
223
+ buff [252 ] = 12
226
224
hostname_len = len (self ._hostname )
227
225
after_hostname = 254 + hostname_len
228
- _BUFF [253 ] = hostname_len
229
- _BUFF [254 :after_hostname ] = self ._hostname
226
+ buff [253 ] = hostname_len
227
+ buff [254 :after_hostname ] = self ._hostname
230
228
231
229
if state == _DHCP_REQUEST and not renew :
232
230
# Set the parsed local IP addr
233
- _BUFF [after_hostname ] = 50
234
- _BUFF [after_hostname + 1 ] = 0x04
235
- _BUFF [after_hostname + 2 : after_hostname + 6 ] = bytes (self .local_ip )
231
+ buff [after_hostname ] = 50
232
+ buff [after_hostname + 1 ] = 0x04
233
+ buff [after_hostname + 2 : after_hostname + 6 ] = bytes (self .local_ip )
236
234
# Set the parsed dhcp server ip addr
237
- _BUFF [after_hostname + 6 ] = 54
238
- _BUFF [after_hostname + 7 ] = 0x04
239
- _BUFF [after_hostname + 8 : after_hostname + 12 ] = bytes (self .dhcp_server_ip )
235
+ buff [after_hostname + 6 ] = 54
236
+ buff [after_hostname + 7 ] = 0x04
237
+ buff [after_hostname + 8 : after_hostname + 12 ] = bytes (self .dhcp_server_ip )
240
238
241
- _BUFF [after_hostname + 12 ] = 55
242
- _BUFF [after_hostname + 13 ] = 0x06
239
+ buff [after_hostname + 12 ] = 55
240
+ buff [after_hostname + 13 ] = 0x06
243
241
# subnet mask
244
- _BUFF [after_hostname + 14 ] = 1
242
+ buff [after_hostname + 14 ] = 1
245
243
# routers on subnet
246
- _BUFF [after_hostname + 15 ] = 3
244
+ buff [after_hostname + 15 ] = 3
247
245
# DNS
248
- _BUFF [after_hostname + 16 ] = 6
246
+ buff [after_hostname + 16 ] = 6
249
247
# domain name
250
- _BUFF [after_hostname + 17 ] = 15
248
+ buff [after_hostname + 17 ] = 15
251
249
# renewal (T1) value
252
- _BUFF [after_hostname + 18 ] = 58
250
+ buff [after_hostname + 18 ] = 58
253
251
# rebinding (T2) value
254
- _BUFF [after_hostname + 19 ] = 59
255
- _BUFF [after_hostname + 20 ] = 255
252
+ buff [after_hostname + 19 ] = 59
253
+ buff [after_hostname + 20 ] = 255
256
254
257
255
# Send DHCP packet
258
- self ._sock .send (_BUFF )
256
+ self ._sock .send (buff )
259
257
260
258
# pylint: disable=too-many-branches, too-many-statements
261
259
def parse_dhcp_response (
@@ -265,89 +263,88 @@ def parse_dhcp_response(
265
263
266
264
:return Tuple[int, bytearray]: DHCP packet type and ID.
267
265
"""
268
- global _BUFF # pylint: disable=global-statement
269
266
# store packet in buffer
270
- _BUFF = bytearray (self ._sock .recv (len ( _BUFF ) ))
267
+ buff = bytearray (self ._sock .recv (_BUFF_SIZE ))
271
268
if self ._debug :
272
- print ("DHCP Response: " , _BUFF )
269
+ print ("DHCP Response: " , buff )
273
270
274
271
# -- Parse Packet, FIXED -- #
275
272
# Validate OP
276
- if _BUFF [0 ] != _DHCP_BOOT_REPLY :
273
+ if buff [0 ] != _DHCP_BOOT_REPLY :
277
274
raise RuntimeError (
278
275
"Malformed Packet - \
279
276
DHCP message OP is not expected BOOT Reply."
280
277
)
281
278
282
- xid = _BUFF [4 :8 ]
279
+ xid = buff [4 :8 ]
283
280
if bytes (xid ) != self ._initial_xid :
284
281
raise ValueError ("DHCP response ID mismatch." )
285
282
286
- self .local_ip = tuple (_BUFF [16 :20 ])
283
+ self .local_ip = tuple (buff [16 :20 ])
287
284
# Check that there is a server ID.
288
- if _BUFF [28 :34 ] == b"\x00 \x00 \x00 \x00 \x00 \x00 " :
285
+ if buff [28 :34 ] == b"\x00 \x00 \x00 \x00 \x00 \x00 " :
289
286
raise ValueError ("No DHCP server ID in the response." )
290
287
291
- if _BUFF [236 :240 ] != _MAGIC_COOKIE :
288
+ if buff [236 :240 ] != _MAGIC_COOKIE :
292
289
raise ValueError ("No DHCP Magic Cookie in the response." )
293
290
294
291
# -- Parse Packet, VARIABLE -- #
295
292
ptr = 240
296
- while _BUFF [ptr ] != _OPT_END :
297
- if _BUFF [ptr ] == _MSG_TYPE :
293
+ while buff [ptr ] != _OPT_END :
294
+ if buff [ptr ] == _MSG_TYPE :
298
295
ptr += 1
299
- opt_len = _BUFF [ptr ]
296
+ opt_len = buff [ptr ]
300
297
ptr += opt_len
301
- msg_type = _BUFF [ptr ]
298
+ msg_type = buff [ptr ]
302
299
ptr += 1
303
- elif _BUFF [ptr ] == _SUBNET_MASK :
300
+ elif buff [ptr ] == _SUBNET_MASK :
304
301
ptr += 1
305
- opt_len = _BUFF [ptr ]
302
+ opt_len = buff [ptr ]
306
303
ptr += 1
307
- self .subnet_mask = tuple (_BUFF [ptr : ptr + opt_len ])
304
+ self .subnet_mask = tuple (buff [ptr : ptr + opt_len ])
308
305
ptr += opt_len
309
- elif _BUFF [ptr ] == _DHCP_SERVER_ID :
306
+ elif buff [ptr ] == _DHCP_SERVER_ID :
310
307
ptr += 1
311
- opt_len = _BUFF [ptr ]
308
+ opt_len = buff [ptr ]
312
309
ptr += 1
313
- self .dhcp_server_ip = tuple (_BUFF [ptr : ptr + opt_len ])
310
+ self .dhcp_server_ip = tuple (buff [ptr : ptr + opt_len ])
314
311
ptr += opt_len
315
- elif _BUFF [ptr ] == _LEASE_TIME :
312
+ elif buff [ptr ] == _LEASE_TIME :
316
313
ptr += 1
317
- opt_len = _BUFF [ptr ]
314
+ opt_len = buff [ptr ]
318
315
ptr += 1
319
- self ._lease_time = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "big" )
316
+ self ._lease_time = int .from_bytes (buff [ptr : ptr + opt_len ], "big" )
320
317
ptr += opt_len
321
- elif _BUFF [ptr ] == _ROUTERS_ON_SUBNET :
318
+ elif buff [ptr ] == _ROUTERS_ON_SUBNET :
322
319
ptr += 1
323
- opt_len = _BUFF [ptr ]
320
+ opt_len = buff [ptr ]
324
321
ptr += 1
325
- self .gateway_ip = tuple (_BUFF [ptr : ptr + 4 ])
322
+ self .gateway_ip = tuple (buff [ptr : ptr + 4 ])
326
323
ptr += opt_len
327
- elif _BUFF [ptr ] == _DNS_SERVERS :
324
+ elif buff [ptr ] == _DNS_SERVERS :
328
325
ptr += 1
329
- opt_len = _BUFF [ptr ]
326
+ opt_len = buff [ptr ]
330
327
ptr += 1
331
- self .dns_server_ip = tuple (_BUFF [ptr : ptr + 4 ])
328
+ self .dns_server_ip = tuple (buff [ptr : ptr + 4 ])
332
329
ptr += opt_len # still increment even though we only read 1 addr.
333
- elif _BUFF [ptr ] == _T1_VAL :
330
+ elif buff [ptr ] == _T1_VAL :
334
331
ptr += 1
335
- opt_len = _BUFF [ptr ]
332
+ opt_len = buff [ptr ]
336
333
ptr += 1
337
- self ._t1 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "big" )
334
+ self ._t1 = int .from_bytes (buff [ptr : ptr + opt_len ], "big" )
338
335
ptr += opt_len
339
- elif _BUFF [ptr ] == _T2_VAL :
336
+ elif buff [ptr ] == _T2_VAL :
340
337
ptr += 1
341
- opt_len = _BUFF [ptr ]
338
+ opt_len = buff [ptr ]
342
339
ptr += 1
343
- self ._t2 = int .from_bytes (_BUFF [ptr : ptr + opt_len ], "big" )
340
+ self ._t2 = int .from_bytes (buff [ptr : ptr + opt_len ], "big" )
344
341
ptr += opt_len
345
- elif _BUFF [ptr ] == 0 :
342
+ elif buff [ptr ] == 0 :
346
343
break
347
344
else :
348
345
# We're not interested in this option
349
346
ptr += 1
350
- opt_len = _BUFF [ptr ]
347
+ opt_len = buff [ptr ]
351
348
ptr += 1
352
349
# no-op
353
350
ptr += opt_len
0 commit comments