75
75
BROADCAST_SERVER_ADDR = 255 , 255 , 255 , 255
76
76
77
77
# pylint: enable=bad-whitespace
78
-
79
- _BUFF = bytearray (317 )
78
+ _buff = bytearray (317 )
80
79
81
80
class DHCP :
82
81
"""W5k DHCP Client implementation.
@@ -87,10 +86,9 @@ class DHCP:
87
86
:param bool debug: Enable debugging output.
88
87
89
88
"""
90
-
89
+
91
90
# pylint: disable=too-many-arguments, too-many-instance-attributes
92
91
def __init__ (self , eth , mac_address , timeout = 1 , timeout_response = 1 ):
93
-
94
92
self ._timeout = timeout
95
93
self ._response_timeout = timeout_response
96
94
self ._mac_address = mac_address
@@ -126,151 +124,149 @@ def send_dhcp_message(self, state, time_elapsed):
126
124
attempted to acquire/renew a lease.
127
125
"""
128
126
# OP
129
- _BUFF [0 ] = DHCP_BOOT_REQUEST
127
+ _buff [0 ] = DHCP_BOOT_REQUEST
130
128
# HTYPE
131
- _BUFF [1 ] = DHCP_HTYPE10MB
129
+ _buff [1 ] = DHCP_HTYPE10MB
132
130
# HLEN
133
- _BUFF [2 ] = DHCP_HLENETHERNET
131
+ _buff [2 ] = DHCP_HLENETHERNET
134
132
# HOPS
135
- _BUFF [3 ] = DHCP_HOPS
133
+ _buff [3 ] = DHCP_HOPS
136
134
137
135
# Transaction ID (xid)
138
136
self ._initial_xid = htonl (self ._transaction_id )
139
137
self ._initial_xid = self ._initial_xid .to_bytes (4 , 'l' )
140
- _BUFF [4 :7 ] = self ._initial_xid
138
+ _buff [4 :7 ] = self ._initial_xid
141
139
142
140
# seconds elapsed
143
- _BUFF [8 ] = ((int (time_elapsed ) & 0xff00 ) >> 8 )
144
- _BUFF [9 ] = (int (time_elapsed ) & 0x00ff )
141
+ _buff [8 ] = ((int (time_elapsed ) & 0xff00 ) >> 8 )
142
+ _buff [9 ] = (int (time_elapsed ) & 0x00ff )
145
143
146
144
# flags
147
145
flags = htons (0x8000 )
148
146
flags = flags .to_bytes (2 , 'b' )
149
- _BUFF [10 ] = flags [1 ]
150
- _BUFF [11 ] = flags [0 ]
147
+ _buff [10 ] = flags [1 ]
148
+ _buff [11 ] = flags [0 ]
151
149
152
150
# NOTE: Skipping cidaddr/yiaddr/siaddr/giaddr
153
151
# as they're already set to 0.0.0.0
154
152
155
153
# chaddr
156
- _BUFF [28 :34 ] = self ._mac_address
154
+ _buff [28 :34 ] = self ._mac_address
157
155
158
156
# NOTE: 192 octets of 0's, BOOTP legacy
159
157
160
158
# Magic Cookie
161
- _BUFF [236 ] = ((MAGIC_COOKIE >> 24 )& 0xFF )
162
- _BUFF [237 ] = ((MAGIC_COOKIE >> 16 )& 0xFF )
163
- _BUFF [238 ] = ((MAGIC_COOKIE >> 8 )& 0xFF )
164
- _BUFF [239 ] = (MAGIC_COOKIE & 0xFF )
159
+ _buff [236 ] = ((MAGIC_COOKIE >> 24 )& 0xFF )
160
+ _buff [237 ] = ((MAGIC_COOKIE >> 16 )& 0xFF )
161
+ _buff [238 ] = ((MAGIC_COOKIE >> 8 )& 0xFF )
162
+ _buff [239 ] = (MAGIC_COOKIE & 0xFF )
165
163
166
164
# Option - DHCP Message Type
167
- _BUFF [240 ] = 53
168
- _BUFF [241 ] = 0x01
169
- _BUFF [242 ] = state
165
+ _buff [240 ] = 53
166
+ _buff [241 ] = 0x01
167
+ _buff [242 ] = state
170
168
171
169
# Option - Client Identifier
172
- _BUFF [243 ] = 61
170
+ _buff [243 ] = 61
173
171
# Length
174
- _BUFF [244 ] = 0x07
172
+ _buff [244 ] = 0x07
175
173
# HW Type - ETH
176
- _BUFF [245 ] = 0x01
174
+ _buff [245 ] = 0x01
177
175
# Client MAC Address
178
176
for mac in range (0 , len (self ._mac_address )):
179
- _BUFF [246 + mac ] = self ._mac_address [mac ]
177
+ _buff [246 + mac ] = self ._mac_address [mac ]
180
178
181
179
# Option - Host Name
182
- _BUFF [252 ] = 12
183
- _BUFF [253 ] = len (b"Wiznet" ) + 6
180
+ _buff [252 ] = 12
181
+ _buff [253 ] = len (b"Wiznet" ) + 6
184
182
# NOTE/TODO: This appends invalid ? chars. onto hostname instead of string
185
- _BUFF [254 :266 ] = b"Wizneteeeeee"
183
+ _buff [254 :266 ] = b"Wizneteeeeee"
186
184
187
185
if state == DHCP_REQUEST :
188
186
# Set the parsed local IP addr
189
- _BUFF [266 ] = 50
190
- _BUFF [267 ] = 0x04
187
+ _buff [266 ] = 50
188
+ _buff [267 ] = 0x04
191
189
192
- _BUFF [268 :272 ] = self .local_ip
190
+ _buff [268 :272 ] = self .local_ip
193
191
# Set the parsed dhcp server ip addr
194
- _BUFF [272 ] = 54
195
- _BUFF [273 ] = 0x04
196
- _BUFF [274 :278 ] = self .dhcp_server_ip
192
+ _buff [272 ] = 54
193
+ _buff [273 ] = 0x04
194
+ _buff [274 :278 ] = self .dhcp_server_ip
197
195
198
- _BUFF [278 ] = 55
199
- _BUFF [279 ] = 0x06
196
+ _buff [278 ] = 55
197
+ _buff [279 ] = 0x06
200
198
# subnet mask
201
- _BUFF [280 ] = 1
199
+ _buff [280 ] = 1
202
200
# routers on subnet
203
- _BUFF [281 ] = 3
201
+ _buff [281 ] = 3
204
202
# DNS
205
- _BUFF [282 ] = 6
203
+ _buff [282 ] = 6
206
204
# domain name
207
- _BUFF [283 ] = 15
205
+ _buff [283 ] = 15
208
206
# renewal (T1) value
209
- _BUFF [284 ] = 58
207
+ _buff [284 ] = 58
210
208
# rebinding (T2) value
211
- _BUFF [285 ] = 59
212
- _BUFF [286 ] = 255
209
+ _buff [285 ] = 59
210
+ _buff [286 ] = 255
213
211
214
212
# Send DHCP packet
215
- self ._sock .send (_BUFF )
213
+ self ._sock .send (_buff )
216
214
217
215
def parse_dhcp_response (self , response_timeout ):
218
216
"""Parse DHCP response from DHCP server.
219
217
Returns DHCP packet type.
220
218
221
219
:param int response_timeout: Time to wait for server to return packet, in seconds.
222
220
"""
223
- print ("CHECKING PACKET SIZE.." )
221
+ print ("STATE: " , self . _dhcp_state )
224
222
start_time = time .monotonic ()
225
- packet_sz = 0
223
+ packet_sz = self . _sock . available ()
226
224
while packet_sz <= 0 :
227
225
packet_sz = self ._sock .available ()
228
226
if (time .monotonic () - start_time ) > response_timeout :
229
227
return 255
230
228
time .sleep (0.05 )
231
- # re-allocate and zero-out global packet buffer
232
- _BUFF = bytearray (packet_sz )
233
- _BUFF = self ._sock .recv (packet_sz )[0 ]
229
+ # store packet in buffer
230
+ _buff = self ._sock .recv (packet_sz )[0 ]
234
231
235
232
# Check OP, if valid, let's parse the packet out!
236
- assert _BUFF [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
233
+ assert _buff [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
237
234
DHCP message OP is not expected BOOT Reply."
238
235
239
-
240
236
# Client Hardware Address (CHADDR)
241
237
chaddr = bytearray (6 )
242
238
for mac , _ in enumerate (chaddr ):
243
- chaddr [mac ] = _BUFF [28 + mac ]
239
+ chaddr [mac ] = _buff [28 + mac ]
244
240
245
241
if chaddr != 0 :
246
- xid = _BUFF [4 :8 ]
242
+ xid = _buff [4 :8 ]
247
243
if bytes (xid ) < self ._initial_xid :
248
244
return 0 , 0
249
245
250
246
# Your IP Address (YIADDR)
251
- self .local_ip = _BUFF [16 :20 ]
247
+ self .local_ip = _buff [16 :20 ]
252
248
253
249
# Gateway IP Address (GIADDR)
254
- self .gateway_ip = _BUFF [20 :24 ]
250
+ self .gateway_ip = _buff [20 :24 ]
255
251
256
252
# NOTE: Next 192 octets are 0's for BOOTP legacy
257
253
258
254
# DHCP Message Type
259
- msg_type = _BUFF [242 ]
255
+ msg_type = _buff [242 ]
260
256
# DHCP Server ID
261
- self .dhcp_server_ip = _BUFF [245 :249 ]
257
+ self .dhcp_server_ip = _buff [245 :249 ]
262
258
# Lease Time, in seconds
263
- self ._lease_time = int .from_bytes (_BUFF [251 :255 ], 'l' )
259
+ self ._lease_time = int .from_bytes (_buff [251 :255 ], 'l' )
264
260
# T1 value
265
- self ._t1 = int .from_bytes (_BUFF [257 :261 ], 'l' )
261
+ self ._t1 = int .from_bytes (_buff [257 :261 ], 'l' )
266
262
# print("T1: ", self._t1)
267
263
# T2 value
268
- self ._t2 = int .from_bytes (_BUFF [263 :267 ], 'l' )
264
+ self ._t2 = int .from_bytes (_buff [263 :267 ], 'l' )
269
265
# print("T2: ", self._t2)
270
266
# Subnet Mask
271
- self .subnet_mask = _BUFF [269 :273 ]
267
+ self .subnet_mask = _buff [269 :273 ]
272
268
# DNS Server
273
- self .dns_server_ip = _BUFF [285 :289 ]
269
+ self .dns_server_ip = _buff [285 :289 ]
274
270
275
271
return msg_type , xid
276
272
@@ -287,21 +283,24 @@ def request_dhcp_lease(self):
287
283
288
284
while self ._dhcp_state != STATE_DHCP_LEASED :
289
285
if self ._dhcp_state == STATE_DHCP_START :
286
+ print ("START" )
290
287
self ._transaction_id += 1
291
288
self ._sock .connect ((BROADCAST_SERVER_ADDR , DHCP_SERVER_PORT ))
292
289
self .send_dhcp_message (STATE_DHCP_DISCOVER ,
293
290
((time .monotonic () - start_time ) / 1000 ))
294
291
self ._dhcp_state = STATE_DHCP_DISCOVER
295
292
elif self ._dhcp_state == STATE_DHCP_DISCOVER :
293
+ print ("DISCOVER" )
296
294
msg_type , xid = self .parse_dhcp_response (self ._timeout )
297
- print (msg_type )
298
295
if msg_type == DHCP_OFFER :
299
296
# use the _transaction_id the offer returned,
300
297
# rather than the current one
301
298
self ._transaction_id = self ._transaction_id .from_bytes (xid , 'l' )
299
+ print ("REQUEST" )
302
300
self .send_dhcp_message (DHCP_REQUEST , ((time .monotonic () - start_time ) / 1000 ))
303
301
self ._dhcp_state = STATE_DHCP_REQUEST
304
302
elif STATE_DHCP_REQUEST :
303
+ print ("LEASE" )
305
304
msg_type , xid = self .parse_dhcp_response (self ._timeout )
306
305
if msg_type == DHCP_ACK :
307
306
self ._dhcp_state = STATE_DHCP_LEASED
0 commit comments