@@ -95,14 +95,14 @@ class DHCP:
95
95
"""W5k DHCP Client implementation.
96
96
:param eth: Wiznet 5k object
97
97
: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.
100
100
:param bool debug: Enable debugging output.
101
101
102
102
"""
103
103
104
104
# 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 ):
106
106
self ._debug = debug
107
107
self ._response_timeout = response_timeout
108
108
self ._mac_address = mac_address
@@ -123,6 +123,7 @@ def __init__(self, eth, mac_address, response_timeout=3, debug=False):
123
123
self .gateway_ip = 0
124
124
self .subnet_mask = 0
125
125
self .dns_server_ip = 0
126
+
126
127
# Lease configuration
127
128
self ._lease_time = 0
128
129
self ._last_check_lease_ms = 0
@@ -131,6 +132,11 @@ def __init__(self, eth, mac_address, response_timeout=3, debug=False):
131
132
self ._t1 = 0
132
133
self ._t2 = 0
133
134
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
+
134
140
def send_dhcp_message (self , state , time_elapsed ):
135
141
"""Assemble and send a DHCP message packet to a socket.
136
142
:param int state: DHCP Message state.
@@ -192,38 +198,37 @@ def send_dhcp_message(self, state, time_elapsed):
192
198
193
199
# Option - Host Name
194
200
_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
200
205
201
206
if state == DHCP_REQUEST :
202
207
# 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
205
210
206
- _BUFF [268 : 272 ] = self .local_ip
211
+ _BUFF [after_hostname + 2 : after_hostname + 6 ] = self .local_ip
207
212
# 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
211
216
212
- _BUFF [278 ] = 55
213
- _BUFF [279 ] = 0x06
217
+ _BUFF [after_hostname + 12 ] = 55
218
+ _BUFF [after_hostname + 13 ] = 0x06
214
219
# subnet mask
215
- _BUFF [280 ] = 1
220
+ _BUFF [after_hostname + 14 ] = 1
216
221
# routers on subnet
217
- _BUFF [281 ] = 3
222
+ _BUFF [after_hostname + 15 ] = 3
218
223
# DNS
219
- _BUFF [282 ] = 6
224
+ _BUFF [after_hostname + 16 ] = 6
220
225
# domain name
221
- _BUFF [283 ] = 15
226
+ _BUFF [after_hostname + 17 ] = 15
222
227
# renewal (T1) value
223
- _BUFF [284 ] = 58
228
+ _BUFF [after_hostname + 18 ] = 58
224
229
# rebinding (T2) value
225
- _BUFF [285 ] = 59
226
- _BUFF [286 ] = 255
230
+ _BUFF [after_hostname + 19 ] = 59
231
+ _BUFF [after_hostname + 20 ] = 255
227
232
228
233
# Send DHCP packet
229
234
self ._sock .send (_BUFF )
0 commit comments