Skip to content

Commit a028c34

Browse files
author
brentru
committed
if initialized, dhcp failing to configure should Raise. Handle invalid xid, magic cookie, MAC within dhcp state machine
1 parent 7db5a2c commit a028c34

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def __init__(self, spi_bus, cs, reset=None,
183183
self._dns = 0
184184
# Set DHCP
185185
if is_dhcp:
186-
self.set_dhcp()
186+
ret = self.set_dhcp()
187+
assert ret == 0, "Failed to configure DHCP Server!"
187188

188189
def set_dhcp(self, response_timeout=1):
189190
"""Initializes the DHCP client and attempts to retrieve
@@ -218,10 +219,7 @@ def set_dhcp(self, response_timeout=1):
218219
_gw_addr,
219220
self._dns))
220221
return 0
221-
if self._debug:
222-
print("* Failed to find DHCP server!")
223-
self._src_port = 0
224-
return 1
222+
return -1
225223

226224
def get_host_by_name(self, hostname):
227225
"""Convert a hostname to a packed 4-byte IP Address.

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
296296
self.gateway_ip = _BUFF[ptr:ptr+opt_len]
297297
ptr += opt_len
298298
elif _BUFF[ptr] == DNS_SERVERS:
299-
ptr += 2 # move past length
300-
# NOTE: we're only using the first DNS server
299+
ptr += 1
301300
opt_len = _BUFF[ptr]
301+
ptr += 1
302302
self.dns_server_ip = _BUFF[ptr:ptr+4]
303303
ptr += opt_len # still increment even though we only read 1 addr.
304304
elif _BUFF[ptr] == T1_VAL:
@@ -333,7 +333,7 @@ def parse_dhcp_response(self, response_timeout): # pylint: disable=too-many-bran
333333
gc.collect()
334334
return msg_type, xid
335335

336-
def request_dhcp_lease(self): # pylint: disable=too-many-branches
336+
def request_dhcp_lease(self): # pylint: disable=too-many-branches, too-many-statements
337337
"""Request to renew or acquire a DHCP lease.
338338
339339
"""
@@ -365,6 +365,8 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches
365365
print("* DHCP: Request")
366366
self.send_dhcp_message(DHCP_REQUEST, ((time.monotonic() - start_time) / 1000))
367367
self._dhcp_state = STATE_DHCP_REQUEST
368+
else:
369+
print("* Received DHCP Message is not OFFER")
368370
elif STATE_DHCP_REQUEST:
369371
if self._debug:
370372
print("* DHCP: Parsing ACK")
@@ -384,6 +386,8 @@ def request_dhcp_lease(self): # pylint: disable=too-many-branches
384386
self._rebind_in_sec = self._t2
385387
elif msg_type == DHCP_NAK:
386388
self._dhcp_state = STATE_DHCP_START
389+
else:
390+
print("* Received DHCP Message is not OFFER")
387391

388392
if msg_type == 255:
389393
msg_type = 0

0 commit comments

Comments
 (0)