diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java index 0326bb97dff..4df1900c0a6 100644 --- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java +++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java @@ -243,6 +243,10 @@ public void close() throws IOException { socket.setSendBufferSize(this.size()); socket.setReceiveBufferSize(64 * 1024); socket.setBroadcast(true); + socket.setReuseAddress(true); + if (multicast != null) { + ((MulticastSocket)socket).setLoopbackMode(false); + } if (multicast == null) { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java index 3442ab28262..87e34a72b3f 100644 --- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java +++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java @@ -25,15 +25,11 @@ import java.io.OutputStream; import java.net.DatagramPacket; import java.net.InetSocketAddress; -import java.net.InterfaceAddress; import java.net.MulticastSocket; import java.net.NetworkInterface; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.URI; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; import java.util.logging.Logger; import org.apache.cxf.Bus; @@ -172,8 +168,12 @@ protected void activate() { socket.setReceiveBufferSize(64 * 1024); socket.setSendBufferSize(64 * 1024); socket.setTimeToLive(1); + socket.setLoopbackMode(false); socket.bind(new InetSocketAddress(isa.getPort())); - socket.setNetworkInterface(findNetworkInterface()); + NetworkInterface ni = findNetworkInterface(); + if (ni != null) { + socket.setNetworkInterface(ni); + } socket.joinGroup(isa.getAddress()); mcast = socket; queue.execute(new MCastListener()); @@ -194,31 +194,15 @@ protected void activate() { throw new RuntimeException(ex); } } + private NetworkInterface findNetworkInterface() throws SocketException { - String name = (String)this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE); + //Allow configuring a default interface, but do not try to guess. + //Instead delegate this to the OS routing table. + String name = (String) this.getEndpointInfo().getProperty(UDPDestination.NETWORK_INTERFACE); NetworkInterface ret = null; if (!StringUtils.isEmpty(name)) { ret = NetworkInterface.getByName(name); } - if (ret == null) { - Enumeration ifcs = NetworkInterface.getNetworkInterfaces(); - List possibles = new ArrayList(); - while (ifcs.hasMoreElements()) { - NetworkInterface ni = ifcs.nextElement(); - if (ni.supportsMulticast() - && ni.isUp()) { - for (InterfaceAddress ia : ni.getInterfaceAddresses()) { - if (ia.getAddress() instanceof java.net.Inet4Address - && !ia.getAddress().isLoopbackAddress() - && !ni.getDisplayName().startsWith("vnic")) { - possibles.add(ni); - } - } - } - } - ret = possibles.isEmpty() ? null : possibles.get(possibles.size() - 1); - - } return ret; } diff --git a/services/ws-discovery/ws-discovery-api/src/test/java/org/apache/cxf/ws/discovery/WSDiscoveryClientTest.java b/services/ws-discovery/ws-discovery-api/src/test/java/org/apache/cxf/ws/discovery/WSDiscoveryClientTest.java index f44217d5e78..4a31ca930e3 100644 --- a/services/ws-discovery/ws-discovery-api/src/test/java/org/apache/cxf/ws/discovery/WSDiscoveryClientTest.java +++ b/services/ws-discovery/ws-discovery-api/src/test/java/org/apache/cxf/ws/discovery/WSDiscoveryClientTest.java @@ -22,14 +22,11 @@ import java.io.InputStream; import java.net.DatagramPacket; import java.net.InetAddress; -import java.net.InterfaceAddress; import java.net.MulticastSocket; import java.net.NetworkInterface; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import javax.jws.WebMethod; import javax.jws.WebService; @@ -59,25 +56,6 @@ public final class WSDiscoveryClientTest { public static final String PORT = TestUtil.getPortNumber(WSDiscoveryClientTest.class); - static NetworkInterface findIpv4Interface() throws Exception { - Enumeration ifcs = NetworkInterface.getNetworkInterfaces(); - List possibles = new ArrayList(); - while (ifcs.hasMoreElements()) { - NetworkInterface ni = ifcs.nextElement(); - if (ni.supportsMulticast() - && ni.isUp()) { - for (InterfaceAddress ia : ni.getInterfaceAddresses()) { - if (ia.getAddress() instanceof java.net.Inet4Address - && !ia.getAddress().isLoopbackAddress() - && !ni.getDisplayName().startsWith("vnic")) { - possibles.add(ni); - } - } - } - } - return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1); - } - @Test public void testMultiResponses() throws Exception { // Disable the test on Redhat Enterprise Linux which doesn't enable the UDP broadcast by default @@ -91,7 +69,7 @@ public void testMultiResponses() throws Exception { int count = 0; while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); - if (!networkInterface.isUp() || networkInterface.isLoopback()) { + if (!networkInterface.isUp()) { continue; } count++; @@ -109,7 +87,8 @@ public void run() { InetAddress address = InetAddress.getByName("239.255.255.250"); MulticastSocket s = new MulticastSocket(Integer.parseInt(PORT)); s.setBroadcast(true); - s.setNetworkInterface(findIpv4Interface()); + s.setLoopbackMode(false); + s.setReuseAddress(true); s.joinGroup(address); s.setReceiveBufferSize(64 * 1024); s.setSoTimeout(5000);