121
121
# UDP socket struct.
122
122
UDP_SOCK = {"bytes_remaining" : 0 , "remote_ip" : 0 , "remote_port" : 0 }
123
123
124
+ # Source ports in use
125
+ SRC_PORTS = [0 ] * W5200_W5500_MAX_SOCK_NUM
126
+
124
127
125
128
class WIZNET5K : # pylint: disable=too-many-public-methods
126
129
"""Interface for WIZNET5K module.
@@ -173,18 +176,21 @@ def __init__(
173
176
assert self ._w5100_init () == 1 , "Failed to initialize WIZnet module."
174
177
# Set MAC address
175
178
self .mac_address = mac
176
- self ._src_port = 0
179
+ self .src_port = 0
177
180
self ._dns = 0
178
181
179
182
# Set DHCP
183
+ self ._dhcp_client = None
180
184
if is_dhcp :
181
185
ret = self .set_dhcp (hostname , dhcp_timeout )
186
+ if ret != 0 :
187
+ self ._dhcp_client = None
182
188
assert ret == 0 , "Failed to configure DHCP Server!"
183
189
184
- def set_dhcp (self , hostname = None , response_timeout = 3 ):
190
+ def set_dhcp (self , hostname = None , response_timeout = 30 ):
185
191
"""Initializes the DHCP client and attempts to retrieve
186
192
and set network configuration from the DHCP server.
187
- Returns True if DHCP configured, False otherwise.
193
+ Returns 0 if DHCP configured, -1 otherwise.
188
194
:param str hostname: The desired hostname, with optional {} to fill in MAC.
189
195
:param int response_timeout: Time to wait for server to return packet, in seconds.
190
196
@@ -202,52 +208,28 @@ def set_dhcp(self, hostname=None, response_timeout=3):
202
208
if self ._debug :
203
209
print ("My Link is:" , self .link_status )
204
210
205
- self ._src_port = 68
206
211
# Return IP assigned by DHCP
207
- _dhcp_client = dhcp .DHCP (
212
+ self . _dhcp_client = dhcp .DHCP (
208
213
self , self .mac_address , hostname , response_timeout , debug = self ._debug
209
214
)
210
- ret = _dhcp_client .request_dhcp_lease ()
215
+ ret = self . _dhcp_client .request_dhcp_lease ()
211
216
if ret == 1 :
212
- _ip = (
213
- _dhcp_client .local_ip [0 ],
214
- _dhcp_client .local_ip [1 ],
215
- _dhcp_client .local_ip [2 ],
216
- _dhcp_client .local_ip [3 ],
217
- )
218
-
219
- _subnet_mask = (
220
- _dhcp_client .subnet_mask [0 ],
221
- _dhcp_client .subnet_mask [1 ],
222
- _dhcp_client .subnet_mask [2 ],
223
- _dhcp_client .subnet_mask [3 ],
224
- )
225
-
226
- _gw_addr = (
227
- _dhcp_client .gateway_ip [0 ],
228
- _dhcp_client .gateway_ip [1 ],
229
- _dhcp_client .gateway_ip [2 ],
230
- _dhcp_client .gateway_ip [3 ],
231
- )
232
-
233
- self ._dns = (
234
- _dhcp_client .dns_server_ip [0 ],
235
- _dhcp_client .dns_server_ip [1 ],
236
- _dhcp_client .dns_server_ip [2 ],
237
- _dhcp_client .dns_server_ip [3 ],
238
- )
239
- self .ifconfig = (_ip , _subnet_mask , _gw_addr , self ._dns )
240
217
if self ._debug :
218
+ _ifconfig = self .ifconfig
241
219
print ("* Found DHCP Server:" )
242
220
print (
243
221
"IP: {}\n Subnet Mask: {}\n GW Addr: {}\n DNS Server: {}" .format (
244
- _ip , _subnet_mask , _gw_addr , self . _dns
222
+ * _ifconfig
245
223
)
246
224
)
247
- self ._src_port = 0
248
225
return 0
249
226
return - 1
250
227
228
+ def maintain_dhcp_lease (self ):
229
+ """Maintain DHCP lease"""
230
+ if self ._dhcp_client is not None :
231
+ self ._dhcp_client .maintain_dhcp_lease ()
232
+
251
233
def get_host_by_name (self , hostname ):
252
234
"""Convert a hostname to a packed 4-byte IP Address.
253
235
Returns a 4 bytearray.
@@ -256,14 +238,12 @@ def get_host_by_name(self, hostname):
256
238
print ("* Get host by name" )
257
239
if isinstance (hostname , str ):
258
240
hostname = bytes (hostname , "utf-8" )
259
- self ._src_port = int (time .monotonic ()) & 0xFFFF
260
241
# Return IP assigned by DHCP
261
242
_dns_client = dns .DNS (self , self ._dns , debug = self ._debug )
262
243
ret = _dns_client .gethostbyname (hostname )
263
244
if self ._debug :
264
245
print ("* Resolved IP: " , ret )
265
246
assert ret != - 1 , "Failed to resolve hostname!"
266
- self ._src_port = 0
267
247
return ret
268
248
269
249
@property
@@ -481,7 +461,11 @@ def socket_available(self, socket_num, sock_type=SNMR_TCP):
481
461
:param int sock_type: Socket type, defaults to TCP.
482
462
"""
483
463
if self ._debug :
484
- print ("* socket_available called with protocol" , sock_type )
464
+ print (
465
+ "* socket_available called on socket {}, protocol {}" .format (
466
+ socket_num , sock_type
467
+ )
468
+ )
485
469
assert socket_num <= self .max_sockets , "Provided socket exceeds max_sockets."
486
470
487
471
res = self ._get_rx_rcv_size (socket_num )
@@ -561,13 +545,7 @@ def get_socket(self):
561
545
sock = SOCKET_INVALID
562
546
for _sock in range (self .max_sockets ):
563
547
status = self .socket_status (_sock )[0 ]
564
- if status in (
565
- SNSR_SOCK_CLOSED ,
566
- SNSR_SOCK_TIME_WAIT ,
567
- SNSR_SOCK_FIN_WAIT ,
568
- SNSR_SOCK_CLOSE_WAIT ,
569
- SNSR_SOCK_CLOSING ,
570
- ):
548
+ if status == SNSR_SOCK_CLOSED :
571
549
sock = _sock
572
550
break
573
551
@@ -588,15 +566,16 @@ def socket_listen(self, socket_num, port):
588
566
)
589
567
)
590
568
# Initialize a socket and set the mode
591
- self ._src_port = port
569
+ self .src_port = port
592
570
res = self .socket_open (socket_num , conn_mode = SNMR_TCP )
571
+ self .src_port = 0
593
572
if res == 1 :
594
573
raise RuntimeError ("Failed to initalize the socket." )
595
574
# Send listen command
596
575
self ._send_socket_cmd (socket_num , CMD_SOCK_LISTEN )
597
576
# Wait until ready
598
577
status = [SNSR_SOCK_CLOSED ]
599
- while status [0 ] != SNSR_SOCK_LISTEN :
578
+ while status [0 ] not in ( SNSR_SOCK_LISTEN , SNSR_SOCK_ESTABLISHED ) :
600
579
status = self ._read_snsr (socket_num )
601
580
if status [0 ] == SNSR_SOCK_CLOSED :
602
581
raise RuntimeError ("Listening socket closed." )
@@ -639,11 +618,15 @@ def socket_open(self, socket_num, conn_mode=SNMR_TCP):
639
618
self ._write_snmr (socket_num , conn_mode )
640
619
self ._write_snir (socket_num , 0xFF )
641
620
642
- if self ._src_port > 0 :
621
+ if self .src_port > 0 :
643
622
# write to socket source port
644
- self ._write_sock_port (socket_num , self ._src_port )
623
+ self ._write_sock_port (socket_num , self .src_port )
645
624
else :
646
- self ._write_sock_port (socket_num , randint (49152 , 65535 ))
625
+ s_port = randint (49152 , 65535 )
626
+ while s_port in SRC_PORTS :
627
+ s_port = randint (49152 , 65535 )
628
+ self ._write_sock_port (socket_num , s_port )
629
+ SRC_PORTS [socket_num ] = s_port
647
630
648
631
# open socket
649
632
self ._write_sncr (socket_num , CMD_SOCK_OPEN )
0 commit comments