Hi,
I partly rewrote this method because it failed to set an IPv4 address on a
Socket connected to an IPv4 multicast group. This happened because on my system
the interface's IPv6 address seems to come before its IPv4 address and the
method took the first one in the old implementation.

While this doesn't fix the problems with MulticastSockets which should deal with
IPv6 multicast groups (see my mail to classpath@gnu.org) it makes at least the
IPv4 case working completely. However the patch shows what kind of unelegant
code is needed to get stuff working at all in an environment with mixed IP 
versions.

ChangeLog:
2006-10-25  Robert Schuster  <[EMAIL PROTECTED]>

        * java/net/MulticastSocket.java:
        (setNetworkInterface): Rewritten.

cya
Robert
Index: java/net/MulticastSocket.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/net/MulticastSocket.java,v
retrieving revision 1.27
diff -u -r1.27 MulticastSocket.java
--- java/net/MulticastSocket.java	24 Oct 2006 23:32:25 -0000	1.27
+++ java/net/MulticastSocket.java	25 Oct 2006 00:42:42 -0000
@@ -202,13 +202,41 @@
   {
     if (isClosed())
       throw new SocketException("socket is closed");
-
-    Enumeration e = netIf.getInetAddresses();
-
-    if (! e.hasMoreElements())
-      throw new SocketException("no network devices found");
-
-    InetAddress address = (InetAddress) e.nextElement();
+    
+    InetAddress address;
+    if (netIf != null)
+      out:
+      {
+        Enumeration e = netIf.getInetAddresses();
+        if (getLocalAddress() instanceof Inet4Address)
+          {
+            // Search for a IPv4 address.
+            while (e.hasMoreElements())
+              {
+                address = (InetAddress) e.nextElement();
+                if (address instanceof Inet4Address)
+                  break out;
+              }
+            throw new SocketException("interface " + netIf.getName() + " has no IPv6 address");
+          }
+        else if (getLocalAddress() instanceof Inet6Address)
+          {
+            // Search for a IPv6 address.
+            while (e.hasMoreElements())
+              {
+                address = (InetAddress) e.nextElement();
+                if (address instanceof Inet6Address)
+                  break out;
+              }
+            throw new SocketException("interface " + netIf.getName() + " has no IPv6 address");
+          }
+        else
+          throw new SocketException("interface " + netIf.getName() + " has no suitable IP address");
+      }
+    else
+      address = InetAddress.ANY_IF;
+    
+    
     getImpl().setOption(SocketOptions.IP_MULTICAST_IF, address);
   }
 

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to