From 6511bc32f623d7738360de9bd753ded474ec6466 Mon Sep 17 00:00:00 2001 From: Dennis Johnson Date: Sun, 18 May 2025 21:14:04 +0530 Subject: [PATCH 1/3] 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/3] 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/3] 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: