From 6511bc32f623d7738360de9bd753ded474ec6466 Mon Sep 17 00:00:00 2001 From: Dennis Johnson Date: Sun, 18 May 2025 21:14:04 +0530 Subject: [PATCH 1/4] Add sock option SO_REUSEPORT to allow udp multicast on macos --- can/interfaces/udp_multicast/bus.py | 1 + 1 file changed, 1 insertion(+) diff --git a/can/interfaces/udp_multicast/bus.py b/can/interfaces/udp_multicast/bus.py index dd114278c..3805876ca 100644 --- a/can/interfaces/udp_multicast/bus.py +++ b/can/interfaces/udp_multicast/bus.py @@ -274,6 +274,7 @@ def _create_socket(self, address_family: socket.AddressFamily) -> socket.socket: # Allow multiple programs to access that address + port sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # set how to receive timestamps try: From 029f7f264b7d80341103fd4da63692b45705019e Mon Sep 17 00:00:00 2001 From: Dennis Johnson Date: Sun, 18 May 2025 21:29:06 +0530 Subject: [PATCH 2/4] macos doesn't support ioctl SIOCGSTAMP --- can/interfaces/udp_multicast/bus.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/can/interfaces/udp_multicast/bus.py b/can/interfaces/udp_multicast/bus.py index 3805876ca..84cbf263b 100644 --- a/can/interfaces/udp_multicast/bus.py +++ b/can/interfaces/udp_multicast/bus.py @@ -5,6 +5,7 @@ import struct import time import warnings +import platform from typing import List, Optional, Tuple, Union import can @@ -21,6 +22,8 @@ ioctl_supported = False pass +# All ioctls aren't supported on MacOS. +is_macos = platform.system() == "Darwin" log = logging.getLogger(__name__) @@ -402,7 +405,7 @@ def recv( self.max_buffer ) - if ioctl_supported: + if ioctl_supported and not is_macos: result_buffer = ioctl( self._socket.fileno(), SIOCGSTAMP, From 9956d8b9688f35a309a098a094c0c29fa6c1d519 Mon Sep 17 00:00:00 2001 From: Dennis Johnson Date: Mon, 19 May 2025 11:39:42 +0530 Subject: [PATCH 3/4] REUSE PORT option not supported on windows --- can/interfaces/udp_multicast/bus.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/can/interfaces/udp_multicast/bus.py b/can/interfaces/udp_multicast/bus.py index 84cbf263b..687e70143 100644 --- a/can/interfaces/udp_multicast/bus.py +++ b/can/interfaces/udp_multicast/bus.py @@ -1,11 +1,11 @@ import errno import logging +import platform import select import socket import struct import time import warnings -import platform from typing import List, Optional, Tuple, Union import can @@ -22,7 +22,7 @@ ioctl_supported = False pass -# All ioctls aren't supported on MacOS. +# All ioctls aren't supported on MacOS. is_macos = platform.system() == "Darwin" log = logging.getLogger(__name__) @@ -277,7 +277,10 @@ def _create_socket(self, address_family: socket.AddressFamily) -> socket.socket: # Allow multiple programs to access that address + port sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + + # Option not supported on Windows. + if hasattr(socket, "SO_REUSEPORT"): + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) # set how to receive timestamps try: From fd1ed401e13ac3c312b9ae12eebbbd53ecca264d Mon Sep 17 00:00:00 2001 From: Dennis Johnson Date: Thu, 12 Jun 2025 17:45:55 +0530 Subject: [PATCH 4/4] only import ioctl on linux --- can/interfaces/udp_multicast/bus.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/can/interfaces/udp_multicast/bus.py b/can/interfaces/udp_multicast/bus.py index 687e70143..23d6b20f5 100644 --- a/can/interfaces/udp_multicast/bus.py +++ b/can/interfaces/udp_multicast/bus.py @@ -14,16 +14,9 @@ from .utils import check_msgpack_installed, pack_message, unpack_message -ioctl_supported = True - -try: +is_linux = platform.system() == "Linux" +if is_linux: from fcntl import ioctl -except ModuleNotFoundError: # Missing on Windows - ioctl_supported = False - pass - -# All ioctls aren't supported on MacOS. -is_macos = platform.system() == "Darwin" log = logging.getLogger(__name__) @@ -408,7 +401,8 @@ def recv( self.max_buffer ) - if ioctl_supported and not is_macos: + if is_linux: + # This ioctl isn't supported on Darwin & Windows. result_buffer = ioctl( self._socket.fileno(), SIOCGSTAMP,