diff --git a/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java b/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java
index a40f1cd..d7ca3c8 100644
--- a/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java
+++ b/src/main/java/org/ice4j/socket/jdk8/MuxServerSocketChannel.java
@@ -55,6 +55,18 @@ public class MuxServerSocketChannel
*/
private static final int SOCKET_CHANNEL_READ_TIMEOUT
= MuxServerSocketChannelFactory.SOCKET_CHANNEL_READ_TIMEOUT;
+
+ /**
+ * Reference to 0.0.0.0 IPv4 or 0::0 IPv6 address for "wildcard" matching purposes
+ */
+ private static final InetAddress ANY_LOCAL_ADDRESS;
+ static {
+ try {
+ ANY_LOCAL_ADDRESS = InetAddress.getByName("::");
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ }
+ }
/**
* Asserts that t is not null by throwing a
@@ -503,7 +515,21 @@ public class MuxServerSocketChannel
{
aLocalAddr = null;
}
- if (aLocalAddr != null && aLocalAddr.equals(localAddr))
+
+ boolean matches = aLocalAddr != null && aLocalAddr.equals(localAddr);
+
+ // if not the same address, let's see if the cached one is an "anyLocalAddress" and if so let's consider it a match
+ if (!matches && aLocalAddr instanceof InetSocketAddress && localAddr instanceof InetSocketAddress)
+ {
+ InetSocketAddress aLocalInetAddr = (InetSocketAddress) aLocalAddr;
+ InetSocketAddress localInetAddr = (InetSocketAddress) localAddr;
+
+ matches =
+ aLocalInetAddr.getAddress().equals(ANY_LOCAL_ADDRESS) &&
+ aLocalInetAddr.getPort() == localInetAddr.getPort();
+ }
+
+ if (matches)
{
channel = aChannel;
// The whole idea of using (1) a List for
diff --git a/test/org/ice4j/socket/jdk8/MuxServerSocketChannelTest.java b/test/org/ice4j/socket/jdk8/MuxServerSocketChannelTest.java
new file mode 100644
index 0000000..778ca26
--- /dev/null
+++ b/test/org/ice4j/socket/jdk8/MuxServerSocketChannelTest.java
@@ -0,0 +1,44 @@
+package org.ice4j.socket.jdk8;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import org.ice4j.socket.DatagramPacketFilter;
+
+import junit.framework.TestCase;
+
+/**
+ * @author robertoandrade
+ */
+public class MuxServerSocketChannelTest extends TestCase {
+
+ public void testOpenAndBindReuseWhenBindingToAllInterfacesFirst() throws IOException
+ {
+ SocketAddress allInterfaces = new InetSocketAddress("::", 4443);
+ SocketAddress oneInterface = new InetSocketAddress("localhost", 4443);
+
+ MuxServerSocketChannel channelForAllInterfaces =
+ MuxServerSocketChannel.openAndBind(null, allInterfaces, 0, new DatagramPacketFilter()
+ {
+ @Override
+ public boolean accept(DatagramPacket p)
+ {
+ return false;
+ }
+ });
+
+ MuxServerSocketChannel channelForOneInterface =
+ MuxServerSocketChannel.openAndBind(null, oneInterface, 0, new DatagramPacketFilter()
+ {
+ @Override
+ public boolean accept(DatagramPacket p)
+ {
+ return false;
+ }
+ });
+
+ assertEquals(channelForAllInterfaces.getFDVal(), channelForOneInterface.getFDVal());
+ }
+}