@@ -95,23 +95,24 @@ 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 , timeout = 1 , timeout_response = 1 , debug = False ):
105
+ def __init__ (
106
+ self , eth , mac_address , hostname = None , response_timeout = 3 , debug = False
107
+ ):
106
108
self ._debug = debug
107
- self ._timeout = timeout
108
- self ._response_timeout = timeout_response
109
+ self ._response_timeout = response_timeout
109
110
self ._mac_address = mac_address
110
111
111
112
# Initalize a new UDP socket for DHCP
112
113
socket .set_interface (eth )
113
114
self ._sock = socket .socket (type = socket .SOCK_DGRAM )
114
- self ._sock .settimeout (timeout )
115
+ self ._sock .settimeout (response_timeout )
115
116
116
117
# DHCP state machine
117
118
self ._dhcp_state = STATE_DHCP_START
@@ -124,6 +125,7 @@ def __init__(self, eth, mac_address, timeout=1, timeout_response=1, debug=False)
124
125
self .gateway_ip = 0
125
126
self .subnet_mask = 0
126
127
self .dns_server_ip = 0
128
+
127
129
# Lease configuration
128
130
self ._lease_time = 0
129
131
self ._last_check_lease_ms = 0
@@ -132,6 +134,12 @@ def __init__(self, eth, mac_address, timeout=1, timeout_response=1, debug=False)
132
134
self ._t1 = 0
133
135
self ._t2 = 0
134
136
137
+ # Host name
138
+ mac_string = "" .join ("{:02X}" .format (o ) for o in mac_address )
139
+ self ._hostname = bytes (
140
+ (hostname or "WIZnet{}" ).split ("." )[0 ].format (mac_string )[:42 ], "utf-8"
141
+ )
142
+
135
143
def send_dhcp_message (self , state , time_elapsed ):
136
144
"""Assemble and send a DHCP message packet to a socket.
137
145
:param int state: DHCP Message state.
@@ -193,38 +201,37 @@ def send_dhcp_message(self, state, time_elapsed):
193
201
194
202
# Option - Host Name
195
203
_BUFF [252 ] = 12
196
- _BUFF [253 ] = len (b"Wiznet" ) + 6
197
- _BUFF [254 :260 ] = b"WIZnet"
198
-
199
- for mac in range (0 , 5 ):
200
- _BUFF [260 + mac ] = self ._mac_address [mac ]
204
+ hostname_len = len (self ._hostname )
205
+ after_hostname = 254 + hostname_len
206
+ _BUFF [253 ] = hostname_len
207
+ _BUFF [254 :after_hostname ] = self ._hostname
201
208
202
209
if state == DHCP_REQUEST :
203
210
# Set the parsed local IP addr
204
- _BUFF [266 ] = 50
205
- _BUFF [267 ] = 0x04
211
+ _BUFF [after_hostname ] = 50
212
+ _BUFF [after_hostname + 1 ] = 0x04
206
213
207
- _BUFF [268 : 272 ] = self .local_ip
214
+ _BUFF [after_hostname + 2 : after_hostname + 6 ] = self .local_ip
208
215
# Set the parsed dhcp server ip addr
209
- _BUFF [272 ] = 54
210
- _BUFF [273 ] = 0x04
211
- _BUFF [274 : 278 ] = self .dhcp_server_ip
216
+ _BUFF [after_hostname + 6 ] = 54
217
+ _BUFF [after_hostname + 7 ] = 0x04
218
+ _BUFF [after_hostname + 8 : after_hostname + 12 ] = self .dhcp_server_ip
212
219
213
- _BUFF [278 ] = 55
214
- _BUFF [279 ] = 0x06
220
+ _BUFF [after_hostname + 12 ] = 55
221
+ _BUFF [after_hostname + 13 ] = 0x06
215
222
# subnet mask
216
- _BUFF [280 ] = 1
223
+ _BUFF [after_hostname + 14 ] = 1
217
224
# routers on subnet
218
- _BUFF [281 ] = 3
225
+ _BUFF [after_hostname + 15 ] = 3
219
226
# DNS
220
- _BUFF [282 ] = 6
227
+ _BUFF [after_hostname + 16 ] = 6
221
228
# domain name
222
- _BUFF [283 ] = 15
229
+ _BUFF [after_hostname + 17 ] = 15
223
230
# renewal (T1) value
224
- _BUFF [284 ] = 58
231
+ _BUFF [after_hostname + 18 ] = 58
225
232
# rebinding (T2) value
226
- _BUFF [285 ] = 59
227
- _BUFF [286 ] = 255
233
+ _BUFF [after_hostname + 19 ] = 59
234
+ _BUFF [after_hostname + 20 ] = 255
228
235
229
236
# Send DHCP packet
230
237
self ._sock .send (_BUFF )
@@ -373,7 +380,7 @@ def request_dhcp_lease(
373
380
elif self ._dhcp_state == STATE_DHCP_DISCOVER :
374
381
if self ._debug :
375
382
print ("* DHCP: Parsing OFFER" )
376
- msg_type , xid = self .parse_dhcp_response (self ._timeout )
383
+ msg_type , xid = self .parse_dhcp_response (self ._response_timeout )
377
384
if msg_type == DHCP_OFFER :
378
385
# use the _transaction_id the offer returned,
379
386
# rather than the current one
@@ -389,7 +396,7 @@ def request_dhcp_lease(
389
396
elif STATE_DHCP_REQUEST :
390
397
if self ._debug :
391
398
print ("* DHCP: Parsing ACK" )
392
- msg_type , xid = self .parse_dhcp_response (self ._timeout )
399
+ msg_type , xid = self .parse_dhcp_response (self ._response_timeout )
393
400
if msg_type == DHCP_ACK :
394
401
self ._dhcp_state = STATE_DHCP_LEASED
395
402
result = 1
@@ -412,7 +419,9 @@ def request_dhcp_lease(
412
419
msg_type = 0
413
420
self ._dhcp_state = STATE_DHCP_START
414
421
415
- if result != 1 and ((time .monotonic () - start_time > self ._timeout )):
422
+ if result != 1 and (
423
+ (time .monotonic () - start_time > self ._response_timeout )
424
+ ):
416
425
break
417
426
418
427
self ._transaction_id += 1
0 commit comments