Skip to content

Commit 4db91ac

Browse files
authored
Merge pull request #95 from BiffoBear/new_dhcp_state_machine
New dhcp state machine
2 parents 05224ce + 9231943 commit 4db91ac

8 files changed

+1744
-1069
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 253 additions & 138 deletions
Large diffs are not rendered by default.

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 563 additions & 416 deletions
Large diffs are not rendered by default.

adafruit_wiznet5k/adafruit_wiznet5k_dns.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def gethostbyname(self, hostname: bytes) -> Union[int, bytes]:
244244
245245
:return Union[int, bytes] The IPv4 address if successful, -1 otherwise.
246246
"""
247-
248247
if self._dns_server is None:
249248
return _INVALID_SERVER
250249
# build DNS request packet

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import time
2929
from sys import byteorder
3030
from micropython import const
31+
3132
import adafruit_wiznet5k as wiznet5k
3233

3334
# pylint: disable=invalid-name

tests/dhcp_dummy_data.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# SPDX-FileCopyrightText: 2022 Martin Stephens
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""Data for use in test_dhcp_helper_files.py"""
5+
6+
7+
def _pad_message(message_section: bytearray, target_length: int) -> bytearray:
8+
"""Pad the message with 0x00."""
9+
return message_section + bytearray(b"\00" * (target_length - len(message_section)))
10+
11+
12+
def _build_message(message_body: bytearray, message_options: bytearray) -> bytearray:
13+
"""Assemble the padded message and body to make a 318 byte packet. The 'header'
14+
section must be 236 bytes and the entire message must be 318 bytes."""
15+
dhcp_message = _pad_message(message_body, 236) + _pad_message(message_options, 276)
16+
assert len(dhcp_message) == 512
17+
return dhcp_message
18+
19+
20+
# Data for testing send data.
21+
# DHCP DISCOVER messages.
22+
# Default settings (DISCOVER, broadcast=False, default hostname, renew=False)
23+
message = bytearray(
24+
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x17\x00\x00\x00\x00\x00\x00"
25+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x05\x06\x07"
26+
b"\x08\t\x00\x00\x00\x00\x00\x00\x00\x00"
27+
)
28+
options = bytearray(
29+
b"c\x82Sc5\x01\x01\x0c\x12WIZnet040506070809=\x07\x01"
30+
b"\x04\x05\x06\x07\x08\t7\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
31+
)
32+
DHCP_SEND_01 = _build_message(message, options)
33+
34+
message = bytearray(
35+
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x17\x00\x00\x00\x00\x00\x00"
36+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x05\x06\x07"
37+
b"\x08\t"
38+
)
39+
options = bytearray(
40+
b"c\x82Sc5\x01\x01\x0c\x12WIZnet040506070809=\x07\x01"
41+
b"\x04\x05\x06\x07\x08\t7\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
42+
)
43+
DHCP_SEND_02 = _build_message(message, options)
44+
45+
message = bytearray(
46+
b"\x01\x01\x06\x00o\xff\xff\xff\x00#\x80\x00\xc0\xa8\x03\x04"
47+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18#.9DO"
48+
)
49+
options = bytearray(
50+
b"c\x82Sc5\x01\x01\x0c\x04bert=\x07\x01\x18#.9DO7"
51+
b"\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
52+
)
53+
DHCP_SEND_03 = _build_message(message, options)
54+
55+
message = bytearray(
56+
b"\x01\x01\x06\x00o\xff\xff\xff\x00#\x80\x00\x00\x00\x00\x00"
57+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xffa$e*c"
58+
)
59+
options = bytearray(
60+
b"c\x82Sc5\x01\x01\x0c\x05clash=\x07\x01\xffa$e*c7"
61+
b"\x03\x01\x03\x063\x04\x00v\xa7\x00\xff"
62+
)
63+
DHCP_SEND_04 = _build_message(message, options)
64+
65+
# DHCP REQUEST messages.
66+
message = bytearray(
67+
b"\x01\x01\x06\x00o\xff\xff\xff\x00\x10\x80\x00\x00\x00\x00\x00"
68+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xffa$e*c"
69+
)
70+
71+
options = bytearray(
72+
b"c\x82Sc5\x01\x03\x0c\nhelicopter=\x07\x01\xffa$e*c7"
73+
b"\x03\x01\x03\x063\x04\x00v\xa7\x002\x04\n\n\n+6\x04\x91B-\x16\xff"
74+
)
75+
DHCP_SEND_05 = _build_message(message, options)
76+
77+
message = bytearray(
78+
b"\x01\x01\x06\x00o\xff\xff\xff\x00H\x80\x00\x00\x00\x00\x00"
79+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00K?\xa6\x04"
80+
b"\xc8e"
81+
)
82+
83+
options = bytearray(
84+
b"c\x82Sc5\x01\x03\x0c\x12WIZnet4B3FA604C865=\x07\x01K?\xa6"
85+
b"\x04\xc8e7\x03\x01\x03\x063\x04\x00v\xa7\x002\x04def\x046"
86+
b"\x04\xf5\xa6\x05\x0b\xff"
87+
)
88+
DHCP_SEND_06 = _build_message(message, options)
89+
90+
# Data to test response parser.
91+
# Basic case, no extra fields, one each of router and DNS.
92+
message = bytearray(
93+
b"\x02\x00\x00\x00\x7f\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xc0"
94+
b"\xa8\x05\x16\x00\x00\x00\x00\x00\x00\x00\x00\x01\x03\x05\x07\t\x0b"
95+
)
96+
97+
options = bytearray(
98+
b"c\x82Sc5\x01\x02\x01\x04\xc0\xa8\x06\x026\x04\xeao\xde"
99+
b"{3\x04\x00\x01\x01\x00\x03\x04yy\x04\x05\x06\x04\x05\x06"
100+
b'\x07\x08:\x04\x00""\x00;\x04\x0033\x00\xff'
101+
)
102+
GOOD_DATA_01 = _build_message(message, options)
103+
104+
# Complex case, extra field, 2 routers and 2 DNS servers.
105+
message = bytearray(
106+
b"\x02\x00\x00\x004Vx\x9a\x00\x00\x00\x00\x00\x00\x00\x00\x12$@\n\x00\x00"
107+
b"\x00\x00\x00\x00\x00\x00\x01"
108+
)
109+
options = bytearray(
110+
b"c\x82Sc5\x01\x05<\x05\x01\x02\x03\x04\x05\x01\x04\n\x0b"
111+
b"\x07\xde6\x04zN\x91\x03\x03\x08\n\x0b\x0e\x0f\xff\x00"
112+
b"\xff\x00\x06\x08\x13\x11\x0b\x07****3\x04\x00\x00=;:\x04"
113+
b"\x00\x0e\x17@;\x04\x02\x92]\xde\xff"
114+
)
115+
GOOD_DATA_02 = _build_message(message, options)
116+
117+
118+
#
119+
message = bytearray(
120+
b"\x02\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x12$@\n\x00\x00"
121+
b"\x00\x00\x00\x00\x00\x00\x01"
122+
)
123+
options = bytearray(b"c\x82Sc")
124+
BAD_DATA = _build_message(message, options)

0 commit comments

Comments
 (0)