From e6bbb52441f90c0ab02d8c4574f2f437270c2ae9 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Feb 2024 15:15:17 -0600 Subject: [PATCH 1/3] Use SOCK_STREAM & SOCK_DGRAM numbers that match circuitpython core A future enhancement to the core ssl module might require this --- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index e0687a0..f250983 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -145,9 +145,13 @@ def inet_ntoa(ip_address: Union[bytes, bytearray]) -> str: return _the_interface.pretty_ip(ip_address) -SOCK_STREAM = const(0x21) # TCP -_TCP_MODE = 80 -SOCK_DGRAM = const(0x02) # UDP +# These must match circuitpython "socketpoool" values. However, we cannot +# depend on socketpool being importable, so hard-code them here. +SOCK_STREAM = 1 +SOCK_DGRAM = 2 + +_SOCKET_TYPE_TO_WIZNET = b"\0\x21\2" + AF_INET = const(3) _SOCKET_INVALID = const(255) @@ -431,7 +435,7 @@ def connect(self, address: Tuple[str, int]) -> None: self._socknum, _the_interface.unpretty_ip(gethostbyname(address[0])), address[1], - self._sock_type, + _SOCKET_TYPE_TO_WIZNET[self._sock_type], ) _the_interface.src_port = 0 if not result: @@ -674,7 +678,9 @@ def _available(self) -> int: :return int: Number of bytes available. """ - return _the_interface.socket_available(self._socknum, self._sock_type) + return _the_interface.socket_available( + self._socknum, _SOCKET_TYPE_TO_WIZNET[self._sock_type] + ) @_check_socket_closed def settimeout(self, value: Optional[float]) -> None: From 47d5c932aae3e9b980e79649b342ab8675457f2e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 17 Apr 2024 14:03:50 -0500 Subject: [PATCH 2/3] remove unused constants --- adafruit_wiznet5k/adafruit_wiznet5k.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 9b43c76..0e8f130 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -183,10 +183,6 @@ def _unprettyfy(data: str, seperator: str, correct_length: int) -> bytes: class WIZNET5K: # pylint: disable=too-many-public-methods, too-many-instance-attributes """Interface for WIZNET5K module.""" - _TCP_MODE = const(0x21) - _UDP_MODE = const(0x02) - _TLS_MODE = const(0x03) # This is NOT currently implemented - _sockets_reserved = [] # pylint: disable=too-many-arguments From 72fd989383cf91c92d92237e7b184055916d875a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 17 Apr 2024 14:25:38 -0500 Subject: [PATCH 3/3] Add setsockopt implementation This is called by httpserver, and also the ssl support in the core assumes that any socket object provides setsockopt (it passes along calls on wrapped sockets, but eagerly fetches the "setsockopt" property on construction of the SSL wrapper object) --- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index f250983..9a3a4f1 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -152,6 +152,9 @@ def inet_ntoa(ip_address: Union[bytes, bytearray]) -> str: _SOCKET_TYPE_TO_WIZNET = b"\0\x21\2" +SOL_SOCKET = 0xFFF +SO_REUSEADDR = 0x0004 + AF_INET = const(3) _SOCKET_INVALID = const(255) @@ -682,6 +685,20 @@ def _available(self) -> int: self._socknum, _SOCKET_TYPE_TO_WIZNET[self._sock_type] ) + @_check_socket_closed + def setsockopt( # pylint: disable=no-self-use + self, level: int, opt: int, value: any + ) -> None: + """ + Set a socket option. + + Only SOL_SOCKET SO_REUSEADDR is accepted (and the value is ignored). + + Other calls result in OSError.""" + if level == SOL_SOCKET and opt == SO_REUSEADDR: + return + raise OSError + @_check_socket_closed def settimeout(self, value: Optional[float]) -> None: """