Skip to content

Commit c5a051d

Browse files
committed
Fix DHCP hostname, make it user configurable
- DHCP hostname was set wrong, by trying to add the binary version of the MAC, it became corrupt and was rejected by the DHCP server. Default is now fixed, "WIZnetXXXXXXXXXXXX" where X'es are MAC. - DHCP hostname is now settable on WIZNET5K class using 'hostname' keyword argument. If the string contains '{}', the MAC will be inserted in this position.
1 parent 828e9af commit c5a051d

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ class WIZNET5K: # pylint: disable=too-many-public-methods
142142
:param ~busio.SPI spi_bus: The SPI bus the Wiznet module is connected to.
143143
:param ~digitalio.DigitalInOut cs: Chip select pin.
144144
:param ~digitalio.DigitalInOut rst: Optional reset pin.
145-
:param bool dhcp: Whether to start DHCP automatically or not.
146-
:param str mac: The Wiznet's MAC Address.
145+
:param bool is_dhcp: Whether to start DHCP automatically or not.
146+
:param list mac: The Wiznet's MAC Address.
147+
:param str hostname: The desired hostname, with optional {} to fill in MAC.
148+
:param int dhcp_timeout: Timeout in seconds for DHCP response.
147149
:param bool debug: Enable debugging output.
148150
149151
"""
@@ -154,8 +156,8 @@ class WIZNET5K: # pylint: disable=too-many-public-methods
154156

155157
# pylint: disable=too-many-arguments
156158
def __init__(
157-
self, spi_bus, cs, reset=None, is_dhcp=True, mac=DEFAULT_MAC, debug=False,
158-
dhcp_timeout=3
159+
self, spi_bus, cs, reset=None, is_dhcp=True, mac=DEFAULT_MAC,
160+
hostname=None, dhcp_timeout=3, debug=False
159161
):
160162
self._debug = debug
161163
self._chip_type = None
@@ -183,13 +185,14 @@ def __init__(
183185
self._dns = 0
184186
# Set DHCP
185187
if is_dhcp:
186-
ret = self.set_dhcp(dhcp_timeout)
188+
ret = self.set_dhcp(hostname, dhcp_timeout)
187189
assert ret == 0, "Failed to configure DHCP Server!"
188190

189-
def set_dhcp(self, response_timeout=3):
191+
def set_dhcp(self, hostname=None, response_timeout=3):
190192
"""Initializes the DHCP client and attempts to retrieve
191193
and set network configuration from the DHCP server.
192194
Returns True if DHCP configured, False otherwise.
195+
:param str hostname: The desired hostname, with optional {} to fill in MAC.
193196
:param int response_timeout: Time to wait for server to return packet, in seconds.
194197
195198
"""
@@ -198,7 +201,7 @@ def set_dhcp(self, response_timeout=3):
198201
self._src_port = 68
199202
# Return IP assigned by DHCP
200203
_dhcp_client = dhcp.DHCP(
201-
self, self.mac_address, response_timeout, debug=self._debug
204+
self, self.mac_address, hostname, response_timeout, debug=self._debug
202205
)
203206
ret = _dhcp_client.request_dhcp_lease()
204207
if ret == 1:

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ class DHCP:
9595
"""W5k DHCP Client implementation.
9696
:param eth: Wiznet 5k object
9797
:param list mac_address: Hardware MAC.
98-
:param int timeout: Packet parsing timeout.
99-
:param int timeout_response: DHCP Response timeout.
98+
:param str hostname: The desired hostname, with optional {} to fill in MAC.
99+
:param int response_timeout: DHCP Response timeout.
100100
:param bool debug: Enable debugging output.
101101
102102
"""
103103

104104
# pylint: disable=too-many-arguments, too-many-instance-attributes, invalid-name
105-
def __init__(self, eth, mac_address, response_timeout=3, debug=False):
105+
def __init__(self, eth, mac_address, hostname=None, response_timeout=3, debug=False):
106106
self._debug = debug
107107
self._response_timeout = response_timeout
108108
self._mac_address = mac_address
@@ -123,6 +123,7 @@ def __init__(self, eth, mac_address, response_timeout=3, debug=False):
123123
self.gateway_ip = 0
124124
self.subnet_mask = 0
125125
self.dns_server_ip = 0
126+
126127
# Lease configuration
127128
self._lease_time = 0
128129
self._last_check_lease_ms = 0
@@ -131,6 +132,11 @@ def __init__(self, eth, mac_address, response_timeout=3, debug=False):
131132
self._t1 = 0
132133
self._t2 = 0
133134

135+
# Host name
136+
mac_string = "".join("{:02X}".format(o) for o in mac_address)
137+
self._hostname = bytes((hostname or "WIZnet{}")
138+
.split('.')[0].format(mac_string)[:42], "utf-8")
139+
134140
def send_dhcp_message(self, state, time_elapsed):
135141
"""Assemble and send a DHCP message packet to a socket.
136142
:param int state: DHCP Message state.
@@ -192,38 +198,37 @@ def send_dhcp_message(self, state, time_elapsed):
192198

193199
# Option - Host Name
194200
_BUFF[252] = 12
195-
_BUFF[253] = len(b"Wiznet") + 6
196-
_BUFF[254:260] = b"WIZnet"
197-
198-
for mac in range(0, 5):
199-
_BUFF[260 + mac] = self._mac_address[mac]
201+
hostname_len = len(self._hostname)
202+
after_hostname = 254 + hostname_len
203+
_BUFF[253] = hostname_len
204+
_BUFF[254:after_hostname] = self._hostname
200205

201206
if state == DHCP_REQUEST:
202207
# Set the parsed local IP addr
203-
_BUFF[266] = 50
204-
_BUFF[267] = 0x04
208+
_BUFF[after_hostname] = 50
209+
_BUFF[after_hostname+1] = 0x04
205210

206-
_BUFF[268:272] = self.local_ip
211+
_BUFF[after_hostname+2:after_hostname+6] = self.local_ip
207212
# Set the parsed dhcp server ip addr
208-
_BUFF[272] = 54
209-
_BUFF[273] = 0x04
210-
_BUFF[274:278] = self.dhcp_server_ip
213+
_BUFF[after_hostname+6] = 54
214+
_BUFF[after_hostname+7] = 0x04
215+
_BUFF[after_hostname+8:after_hostname+12] = self.dhcp_server_ip
211216

212-
_BUFF[278] = 55
213-
_BUFF[279] = 0x06
217+
_BUFF[after_hostname+12] = 55
218+
_BUFF[after_hostname+13] = 0x06
214219
# subnet mask
215-
_BUFF[280] = 1
220+
_BUFF[after_hostname+14] = 1
216221
# routers on subnet
217-
_BUFF[281] = 3
222+
_BUFF[after_hostname+15] = 3
218223
# DNS
219-
_BUFF[282] = 6
224+
_BUFF[after_hostname+16] = 6
220225
# domain name
221-
_BUFF[283] = 15
226+
_BUFF[after_hostname+17] = 15
222227
# renewal (T1) value
223-
_BUFF[284] = 58
228+
_BUFF[after_hostname+18] = 58
224229
# rebinding (T2) value
225-
_BUFF[285] = 59
226-
_BUFF[286] = 255
230+
_BUFF[after_hostname+19] = 59
231+
_BUFF[after_hostname+20] = 255
227232

228233
# Send DHCP packet
229234
self._sock.send(_BUFF)

0 commit comments

Comments
 (0)