changeset 26486:718b4d0243ab

8057556: JDP should better handle non-active interfaces Summary: skip interface if it's not up or doesn't have IP address Reviewed-by: jbachorik Contributed-by: Yasumasa Suenaga <yasuenag@gmail.com>
author dsamersoff
date Thu, 11 Sep 2014 06:21:10 -0700
parents 8ac4566bf6e4
children 605e2ffaec14
files jdk/src/java.management/share/classes/sun/management/jdp/JdpBroadcaster.java
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/java.management/share/classes/sun/management/jdp/JdpBroadcaster.java	Thu Sep 11 15:13:37 2014 +0400
+++ b/jdk/src/java.management/share/classes/sun/management/jdp/JdpBroadcaster.java	Thu Sep 11 06:21:10 2014 -0700
@@ -35,6 +35,7 @@
 import java.nio.ByteBuffer;
 import java.nio.channels.DatagramChannel;
 import java.nio.channels.UnsupportedAddressTypeException;
+import java.util.Enumeration;
 
 /**
  * JdpBroadcaster is responsible for sending pre-built JDP packet across a Net
@@ -79,13 +80,49 @@
         if (srcAddress != null) {
             // User requests particular interface to bind to
             NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
+
+            if (interf == null) {
+                throw new JdpException("Unable to get network interface for " + srcAddress.toString());
+            }
+
+            if (!interf.isUp()) {
+                throw new JdpException(interf.getName() + " is not up.");
+            }
+
+            if (!interf.supportsMulticast()) {
+                throw new JdpException(interf.getName() + " does not support multicast.");
+            }
+
             try {
                 channel.bind(new InetSocketAddress(srcAddress, 0));
             } catch (UnsupportedAddressTypeException ex) {
                 throw new JdpException("Unable to bind to source address");
             }
             channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
+        } else {
+            Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
+            boolean succeed = false;
+
+            while (nics.hasMoreElements()) {
+                NetworkInterface nic = nics.nextElement();
+
+                if (nic.isUp() && nic.supportsMulticast()) {
+                    try {
+                        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, nic);
+                        succeed = true;
+                    } catch (IOException ex) {
+                        // pass
+                    }
+                }
+
+            }
+
+            if (!succeed) {
+                throw new JdpException("Unable to bind to any interfaces.");
+            }
+
         }
+
     }
 
     /**