75
75
BROADCAST_SERVER_ADDR = 255 , 255 , 255 , 255
76
76
77
77
# pylint: enable=bad-whitespace
78
- _buff = bytearray (317 )
78
+ _BUFF = bytearray (317 )
79
79
80
80
class DHCP :
81
81
"""W5k DHCP Client implementation.
@@ -87,7 +87,7 @@ class DHCP:
87
87
88
88
"""
89
89
90
- # pylint: disable=too-many-arguments, too-many-instance-attributes
90
+ # pylint: disable=too-many-arguments, too-many-instance-attributes, invalid-name
91
91
def __init__ (self , eth , mac_address , timeout = 1 , timeout_response = 1 ):
92
92
self ._timeout = timeout
93
93
self ._response_timeout = timeout_response
@@ -124,95 +124,95 @@ def send_dhcp_message(self, state, time_elapsed):
124
124
attempted to acquire/renew a lease.
125
125
"""
126
126
# OP
127
- _buff [0 ] = DHCP_BOOT_REQUEST
127
+ _BUFF [0 ] = DHCP_BOOT_REQUEST
128
128
# HTYPE
129
- _buff [1 ] = DHCP_HTYPE10MB
129
+ _BUFF [1 ] = DHCP_HTYPE10MB
130
130
# HLEN
131
- _buff [2 ] = DHCP_HLENETHERNET
131
+ _BUFF [2 ] = DHCP_HLENETHERNET
132
132
# HOPS
133
- _buff [3 ] = DHCP_HOPS
133
+ _BUFF [3 ] = DHCP_HOPS
134
134
135
135
# Transaction ID (xid)
136
136
self ._initial_xid = htonl (self ._transaction_id )
137
137
self ._initial_xid = self ._initial_xid .to_bytes (4 , 'l' )
138
- _buff [4 :7 ] = self ._initial_xid
138
+ _BUFF [4 :7 ] = self ._initial_xid
139
139
140
140
# seconds elapsed
141
- _buff [8 ] = ((int (time_elapsed ) & 0xff00 ) >> 8 )
142
- _buff [9 ] = (int (time_elapsed ) & 0x00ff )
141
+ _BUFF [8 ] = ((int (time_elapsed ) & 0xff00 ) >> 8 )
142
+ _BUFF [9 ] = (int (time_elapsed ) & 0x00ff )
143
143
144
144
# flags
145
145
flags = htons (0x8000 )
146
146
flags = flags .to_bytes (2 , 'b' )
147
- _buff [10 ] = flags [1 ]
148
- _buff [11 ] = flags [0 ]
147
+ _BUFF [10 ] = flags [1 ]
148
+ _BUFF [11 ] = flags [0 ]
149
149
150
150
# NOTE: Skipping cidaddr/yiaddr/siaddr/giaddr
151
151
# as they're already set to 0.0.0.0
152
152
153
153
# chaddr
154
- _buff [28 :34 ] = self ._mac_address
154
+ _BUFF [28 :34 ] = self ._mac_address
155
155
156
156
# NOTE: 192 octets of 0's, BOOTP legacy
157
157
158
158
# Magic Cookie
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 )
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 )
163
163
164
164
# Option - DHCP Message Type
165
- _buff [240 ] = 53
166
- _buff [241 ] = 0x01
167
- _buff [242 ] = state
165
+ _BUFF [240 ] = 53
166
+ _BUFF [241 ] = 0x01
167
+ _BUFF [242 ] = state
168
168
169
169
# Option - Client Identifier
170
- _buff [243 ] = 61
170
+ _BUFF [243 ] = 61
171
171
# Length
172
- _buff [244 ] = 0x07
172
+ _BUFF [244 ] = 0x07
173
173
# HW Type - ETH
174
- _buff [245 ] = 0x01
174
+ _BUFF [245 ] = 0x01
175
175
# Client MAC Address
176
176
for mac in range (0 , len (self ._mac_address )):
177
- _buff [246 + mac ] = self ._mac_address [mac ]
177
+ _BUFF [246 + mac ] = self ._mac_address [mac ]
178
178
179
179
# Option - Host Name
180
- _buff [252 ] = 12
181
- _buff [253 ] = len (b"Wiznet" ) + 6
182
- _buff [254 :260 ] = b"WIZnet"
180
+ _BUFF [252 ] = 12
181
+ _BUFF [253 ] = len (b"Wiznet" ) + 6
182
+ _BUFF [254 :260 ] = b"WIZnet"
183
183
184
184
for mac in range (0 , 5 ):
185
- _buff [260 + mac ] = self ._mac_address [mac ]
185
+ _BUFF [260 + mac ] = self ._mac_address [mac ]
186
186
187
187
if state == DHCP_REQUEST :
188
188
# Set the parsed local IP addr
189
- _buff [266 ] = 50
190
- _buff [267 ] = 0x04
189
+ _BUFF [266 ] = 50
190
+ _BUFF [267 ] = 0x04
191
191
192
- _buff [268 :272 ] = self .local_ip
192
+ _BUFF [268 :272 ] = self .local_ip
193
193
# Set the parsed dhcp server ip addr
194
- _buff [272 ] = 54
195
- _buff [273 ] = 0x04
196
- _buff [274 :278 ] = self .dhcp_server_ip
194
+ _BUFF [272 ] = 54
195
+ _BUFF [273 ] = 0x04
196
+ _BUFF [274 :278 ] = self .dhcp_server_ip
197
197
198
- _buff [278 ] = 55
199
- _buff [279 ] = 0x06
198
+ _BUFF [278 ] = 55
199
+ _BUFF [279 ] = 0x06
200
200
# subnet mask
201
- _buff [280 ] = 1
201
+ _BUFF [280 ] = 1
202
202
# routers on subnet
203
- _buff [281 ] = 3
203
+ _BUFF [281 ] = 3
204
204
# DNS
205
- _buff [282 ] = 6
205
+ _BUFF [282 ] = 6
206
206
# domain name
207
- _buff [283 ] = 15
207
+ _BUFF [283 ] = 15
208
208
# renewal (T1) value
209
- _buff [284 ] = 58
209
+ _BUFF [284 ] = 58
210
210
# rebinding (T2) value
211
- _buff [285 ] = 59
212
- _buff [286 ] = 255
211
+ _BUFF [285 ] = 59
212
+ _BUFF [286 ] = 255
213
213
214
214
# Send DHCP packet
215
- self ._sock .send (_buff )
215
+ self ._sock .send (_BUFF )
216
216
217
217
def parse_dhcp_response (self , response_timeout ):
218
218
"""Parse DHCP response from DHCP server.
@@ -228,44 +228,44 @@ def parse_dhcp_response(self, response_timeout):
228
228
return 255
229
229
time .sleep (0.05 )
230
230
# store packet in buffer
231
- _buff = self ._sock .recv (packet_sz )[0 ]
231
+ _BUFF = self ._sock .recv (packet_sz )[0 ]
232
232
233
233
# Check OP, if valid, let's parse the packet out!
234
- assert _buff [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
234
+ assert _BUFF [0 ] == DHCP_BOOT_REPLY , "Malformed Packet - \
235
235
DHCP message OP is not expected BOOT Reply."
236
236
237
237
# Client Hardware Address (CHADDR)
238
238
chaddr = bytearray (6 )
239
239
for mac , _ in enumerate (chaddr ):
240
- chaddr [mac ] = _buff [28 + mac ]
240
+ chaddr [mac ] = _BUFF [28 + mac ]
241
241
242
242
if chaddr != 0 :
243
- xid = _buff [4 :8 ]
243
+ xid = _BUFF [4 :8 ]
244
244
if bytes (xid ) < self ._initial_xid :
245
245
return 0 , 0
246
246
247
247
# Your IP Address (YIADDR)
248
- self .local_ip = _buff [16 :20 ]
248
+ self .local_ip = _BUFF [16 :20 ]
249
249
250
250
# Gateway IP Address (GIADDR)
251
- self .gateway_ip = _buff [20 :24 ]
251
+ self .gateway_ip = _BUFF [20 :24 ]
252
252
253
253
# NOTE: Next 192 octets are 0's for BOOTP legacy
254
254
255
255
# DHCP Message Type
256
- msg_type = _buff [242 ]
256
+ msg_type = _BUFF [242 ]
257
257
# DHCP Server ID
258
- self .dhcp_server_ip = _buff [245 :249 ]
258
+ self .dhcp_server_ip = _BUFF [245 :249 ]
259
259
# Lease Time, in seconds
260
- self ._lease_time = int .from_bytes (_buff [251 :255 ], 'l' )
260
+ self ._lease_time = int .from_bytes (_BUFF [251 :255 ], 'l' )
261
261
# T1 value
262
- self ._t1 = int .from_bytes (_buff [257 :261 ], 'l' )
262
+ self ._t1 = int .from_bytes (_BUFF [257 :261 ], 'l' )
263
263
# T2 value
264
- self ._t2 = int .from_bytes (_buff [263 :267 ], 'l' )
264
+ self ._t2 = int .from_bytes (_BUFF [263 :267 ], 'l' )
265
265
# Subnet Mask
266
- self .subnet_mask = _buff [269 :273 ]
266
+ self .subnet_mask = _BUFF [269 :273 ]
267
267
# DNS Server
268
- self .dns_server_ip = _buff [285 :289 ]
268
+ self .dns_server_ip = _BUFF [285 :289 ]
269
269
270
270
return msg_type , xid
271
271
0 commit comments