Skip to content

Commit 958fc64

Browse files
Fix UDP multicast interface on MacOS (#1940)
* Add sock option SO_REUSEPORT to allow udp multicast on macos * macos doesn't support ioctl SIOCGSTAMP * REUSE PORT option not supported on windows * only import ioctl on linux
1 parent 8970fce commit 958fc64

File tree

1 file changed

+9
-8
lines changed
  • can/interfaces/udp_multicast

1 file changed

+9
-8
lines changed

can/interfaces/udp_multicast/bus.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import errno
22
import logging
3+
import platform
34
import select
45
import socket
56
import struct
@@ -13,14 +14,9 @@
1314

1415
from .utils import is_msgpack_installed, pack_message, unpack_message
1516

16-
ioctl_supported = True
17-
18-
try:
17+
is_linux = platform.system() == "Linux"
18+
if is_linux:
1919
from fcntl import ioctl
20-
except ModuleNotFoundError: # Missing on Windows
21-
ioctl_supported = False
22-
pass
23-
2420

2521
log = logging.getLogger(__name__)
2622

@@ -275,6 +271,10 @@ def _create_socket(self, address_family: socket.AddressFamily) -> socket.socket:
275271
# Allow multiple programs to access that address + port
276272
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
277273

274+
# Option not supported on Windows.
275+
if hasattr(socket, "SO_REUSEPORT"):
276+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
277+
278278
# set how to receive timestamps
279279
try:
280280
sock.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1)
@@ -401,7 +401,8 @@ def recv(
401401
self.max_buffer
402402
)
403403

404-
if ioctl_supported:
404+
if is_linux:
405+
# This ioctl isn't supported on Darwin & Windows.
405406
result_buffer = ioctl(
406407
self._socket.fileno(),
407408
SIOCGSTAMP,

0 commit comments

Comments
 (0)