From 228a01e268c478bf5bb3717aa11ad37728ac4276 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 18 Aug 2021 12:11:48 +0200 Subject: [PATCH] Use StandardSocketOptions#IP_MULTICAST_IF as default source when joing multicast groups (#11585) Motivation: We should use StandardSocketOptions#IP_MULTICAST_IF as default source when joing multicast groups and only try to use the localAddress if this returns null. Modifications: First check if StandardSocketOptions#IP_MULTICAST_IF was set and if so use the network interface when joining mulicast groups Result: Fixes https://github.com/netty/netty/issues/11541 --- .../io/netty/channel/epoll/EpollDatagramChannel.java | 8 +++++--- .../io/netty/channel/kqueue/KQueueDatagramChannel.java | 9 +++++---- .../io/netty/channel/socket/nio/NioDatagramChannel.java | 8 +++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java index 87cf0e39d6..79e1fbfddc 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java @@ -141,9 +141,11 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements @Override public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { try { - return joinGroup( - multicastAddress, - NetworkInterface.getByInetAddress(localAddress().getAddress()), null, promise); + NetworkInterface iface = config().getNetworkInterface(); + if (iface == null) { + iface = NetworkInterface.getByInetAddress(localAddress().getAddress()); + } + return joinGroup(multicastAddress, iface, null, promise); } catch (IOException e) { promise.setFailure(e); } diff --git a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java index 3901d051dd..af99bcc9bd 100644 --- a/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java +++ b/transport-native-kqueue/src/main/java/io/netty/channel/kqueue/KQueueDatagramChannel.java @@ -99,10 +99,11 @@ public final class KQueueDatagramChannel extends AbstractKQueueDatagramChannel i @Override public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { try { - return joinGroup( - multicastAddress, - NetworkInterface.getByInetAddress(localAddress().getAddress()), - null, promise); + NetworkInterface iface = config().getNetworkInterface(); + if (iface == null) { + iface = NetworkInterface.getByInetAddress(localAddress().getAddress()); + } + return joinGroup(multicastAddress, iface, null, promise); } catch (SocketException e) { promise.setFailure(e); } diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index 701e873cd7..1cd356ed9b 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -369,10 +369,12 @@ public final class NioDatagramChannel @Override public ChannelFuture joinGroup(InetAddress multicastAddress, ChannelPromise promise) { try { + NetworkInterface iface = config.getNetworkInterface(); + if (iface == null) { + iface = NetworkInterface.getByInetAddress(localAddress().getAddress()); + } return joinGroup( - multicastAddress, - NetworkInterface.getByInetAddress(localAddress().getAddress()), - null, promise); + multicastAddress, iface, null, promise); } catch (SocketException e) { promise.setFailure(e); }