Skip to content

Commit 34c4ab2

Browse files
committed
Fix for issue #108 - Remove global _BUFF and replace with local buff in both functions that use it
1 parent c666a7b commit 34c4ab2

File tree

1 file changed

+74
-77
lines changed

1 file changed

+74
-77
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@
8585
_LEASE_TIME = 51
8686
_OPT_END = 255
8787

88-
# Packet buffer
88+
# Packet buffer size
8989
_BUFF_SIZE = const(318)
90-
_BUFF = bytearray(_BUFF_SIZE)
91-
9290

9391
class DHCP:
9492
"""W5k DHCP Client implementation."""
@@ -167,95 +165,95 @@ def send_dhcp_message(
167165
:param float time_elapsed: Number of seconds elapsed since DHCP process started
168166
:param bool renew: Set True for renew and rebind, defaults to False
169167
"""
170-
_BUFF = bytearray(_BUFF_SIZE)
168+
buff = bytearray(_BUFF_SIZE)
171169
# OP
172-
_BUFF[0] = _DHCP_BOOT_REQUEST
170+
buff[0] = _DHCP_BOOT_REQUEST
173171
# HTYPE
174-
_BUFF[1] = _DHCP_HTYPE10MB
172+
buff[1] = _DHCP_HTYPE10MB
175173
# HLEN
176-
_BUFF[2] = _DHCP_HLENETHERNET
174+
buff[2] = _DHCP_HLENETHERNET
177175
# HOPS
178-
_BUFF[3] = _DHCP_HOPS
176+
buff[3] = _DHCP_HOPS
179177

180178
# Transaction ID (xid)
181179
self._initial_xid = htonl(self._transaction_id)
182180
self._initial_xid = self._initial_xid.to_bytes(4, "big")
183-
_BUFF[4:8] = self._initial_xid
181+
buff[4:8] = self._initial_xid
184182

185183
# 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
188186

189187
# flags
190188
flags = htons(0x8000)
191189
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]
194192

195193
# NOTE: Skipping ciaddr/yiaddr/siaddr/giaddr
196194
# as they're already set to 0.0.0.0
197195
# Except when renewing, then fill in ciaddr
198196
if renew:
199-
_BUFF[12:16] = bytes(self.local_ip)
197+
buff[12:16] = bytes(self.local_ip)
200198

201199
# chaddr
202-
_BUFF[28:34] = self._mac_address
200+
buff[28:34] = self._mac_address
203201

204202
# NOTE: 192 octets of 0's, BOOTP legacy
205203

206204
# Magic Cookie
207-
_BUFF[236:240] = _MAGIC_COOKIE
205+
buff[236:240] = _MAGIC_COOKIE
208206

209207
# 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
213211

214212
# Option - Client Identifier
215-
_BUFF[243] = 61
213+
buff[243] = 61
216214
# Length
217-
_BUFF[244] = 0x07
215+
buff[244] = 0x07
218216
# HW Type - ETH
219-
_BUFF[245] = 0x01
217+
buff[245] = 0x01
220218
# Client MAC Address
221219
for mac, val in enumerate(self._mac_address):
222-
_BUFF[246 + mac] = val
220+
buff[246 + mac] = val
223221

224222
# Option - Host Name
225-
_BUFF[252] = 12
223+
buff[252] = 12
226224
hostname_len = len(self._hostname)
227225
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
230228

231229
if state == _DHCP_REQUEST and not renew:
232230
# 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)
236234
# 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)
240238

241-
_BUFF[after_hostname + 12] = 55
242-
_BUFF[after_hostname + 13] = 0x06
239+
buff[after_hostname + 12] = 55
240+
buff[after_hostname + 13] = 0x06
243241
# subnet mask
244-
_BUFF[after_hostname + 14] = 1
242+
buff[after_hostname + 14] = 1
245243
# routers on subnet
246-
_BUFF[after_hostname + 15] = 3
244+
buff[after_hostname + 15] = 3
247245
# DNS
248-
_BUFF[after_hostname + 16] = 6
246+
buff[after_hostname + 16] = 6
249247
# domain name
250-
_BUFF[after_hostname + 17] = 15
248+
buff[after_hostname + 17] = 15
251249
# renewal (T1) value
252-
_BUFF[after_hostname + 18] = 58
250+
buff[after_hostname + 18] = 58
253251
# 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
256254

257255
# Send DHCP packet
258-
self._sock.send(_BUFF)
256+
self._sock.send(buff)
259257

260258
# pylint: disable=too-many-branches, too-many-statements
261259
def parse_dhcp_response(
@@ -265,89 +263,88 @@ def parse_dhcp_response(
265263
266264
:return Tuple[int, bytearray]: DHCP packet type and ID.
267265
"""
268-
global _BUFF # pylint: disable=global-statement
269266
# store packet in buffer
270-
_BUFF = bytearray(self._sock.recv(len(_BUFF)))
267+
buff = bytearray(self._sock.recv(_BUFF_SIZE))
271268
if self._debug:
272-
print("DHCP Response: ", _BUFF)
269+
print("DHCP Response: ", buff)
273270

274271
# -- Parse Packet, FIXED -- #
275272
# Validate OP
276-
if _BUFF[0] != _DHCP_BOOT_REPLY:
273+
if buff[0] != _DHCP_BOOT_REPLY:
277274
raise RuntimeError(
278275
"Malformed Packet - \
279276
DHCP message OP is not expected BOOT Reply."
280277
)
281278

282-
xid = _BUFF[4:8]
279+
xid = buff[4:8]
283280
if bytes(xid) != self._initial_xid:
284281
raise ValueError("DHCP response ID mismatch.")
285282

286-
self.local_ip = tuple(_BUFF[16:20])
283+
self.local_ip = tuple(buff[16:20])
287284
# 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":
289286
raise ValueError("No DHCP server ID in the response.")
290287

291-
if _BUFF[236:240] != _MAGIC_COOKIE:
288+
if buff[236:240] != _MAGIC_COOKIE:
292289
raise ValueError("No DHCP Magic Cookie in the response.")
293290

294291
# -- Parse Packet, VARIABLE -- #
295292
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:
298295
ptr += 1
299-
opt_len = _BUFF[ptr]
296+
opt_len = buff[ptr]
300297
ptr += opt_len
301-
msg_type = _BUFF[ptr]
298+
msg_type = buff[ptr]
302299
ptr += 1
303-
elif _BUFF[ptr] == _SUBNET_MASK:
300+
elif buff[ptr] == _SUBNET_MASK:
304301
ptr += 1
305-
opt_len = _BUFF[ptr]
302+
opt_len = buff[ptr]
306303
ptr += 1
307-
self.subnet_mask = tuple(_BUFF[ptr : ptr + opt_len])
304+
self.subnet_mask = tuple(buff[ptr : ptr + opt_len])
308305
ptr += opt_len
309-
elif _BUFF[ptr] == _DHCP_SERVER_ID:
306+
elif buff[ptr] == _DHCP_SERVER_ID:
310307
ptr += 1
311-
opt_len = _BUFF[ptr]
308+
opt_len = buff[ptr]
312309
ptr += 1
313-
self.dhcp_server_ip = tuple(_BUFF[ptr : ptr + opt_len])
310+
self.dhcp_server_ip = tuple(buff[ptr : ptr + opt_len])
314311
ptr += opt_len
315-
elif _BUFF[ptr] == _LEASE_TIME:
312+
elif buff[ptr] == _LEASE_TIME:
316313
ptr += 1
317-
opt_len = _BUFF[ptr]
314+
opt_len = buff[ptr]
318315
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")
320317
ptr += opt_len
321-
elif _BUFF[ptr] == _ROUTERS_ON_SUBNET:
318+
elif buff[ptr] == _ROUTERS_ON_SUBNET:
322319
ptr += 1
323-
opt_len = _BUFF[ptr]
320+
opt_len = buff[ptr]
324321
ptr += 1
325-
self.gateway_ip = tuple(_BUFF[ptr : ptr + 4])
322+
self.gateway_ip = tuple(buff[ptr : ptr + 4])
326323
ptr += opt_len
327-
elif _BUFF[ptr] == _DNS_SERVERS:
324+
elif buff[ptr] == _DNS_SERVERS:
328325
ptr += 1
329-
opt_len = _BUFF[ptr]
326+
opt_len = buff[ptr]
330327
ptr += 1
331-
self.dns_server_ip = tuple(_BUFF[ptr : ptr + 4])
328+
self.dns_server_ip = tuple(buff[ptr : ptr + 4])
332329
ptr += opt_len # still increment even though we only read 1 addr.
333-
elif _BUFF[ptr] == _T1_VAL:
330+
elif buff[ptr] == _T1_VAL:
334331
ptr += 1
335-
opt_len = _BUFF[ptr]
332+
opt_len = buff[ptr]
336333
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")
338335
ptr += opt_len
339-
elif _BUFF[ptr] == _T2_VAL:
336+
elif buff[ptr] == _T2_VAL:
340337
ptr += 1
341-
opt_len = _BUFF[ptr]
338+
opt_len = buff[ptr]
342339
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")
344341
ptr += opt_len
345-
elif _BUFF[ptr] == 0:
342+
elif buff[ptr] == 0:
346343
break
347344
else:
348345
# We're not interested in this option
349346
ptr += 1
350-
opt_len = _BUFF[ptr]
347+
opt_len = buff[ptr]
351348
ptr += 1
352349
# no-op
353350
ptr += opt_len

0 commit comments

Comments
 (0)