src/java.management/share/classes/sun/management/jdp/JdpBroadcaster.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.management.jdp;
  26 
  27 import java.io.IOException;
  28 import java.net.Inet6Address;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.net.NetworkInterface;
  32 import java.net.ProtocolFamily;
  33 import java.net.StandardProtocolFamily;
  34 import java.net.StandardSocketOptions;
  35 import java.nio.ByteBuffer;
  36 import java.nio.channels.DatagramChannel;
  37 import java.nio.channels.UnsupportedAddressTypeException;

  38 
  39 /**
  40  * JdpBroadcaster is responsible for sending pre-built JDP packet across a Net
  41  *
  42  * <p> Multicast group address, port number and ttl have to be chosen on upper
  43  * level and passed to broadcaster constructor. Also it's possible to specify
  44  * source address to broadcast from. </p>
  45  *
  46  * <p>JdpBradcaster doesn't perform any validation on a supplied {@code port} and {@code ttl} because
  47  * the allowed values depend on an operating system setup</p>
  48  *
  49  */
  50 public final class JdpBroadcaster {
  51 
  52     private final InetAddress addr;
  53     private final int port;
  54     private final DatagramChannel channel;
  55 
  56     /**
  57      * Create a new broadcaster


  62      * @param ttl - packet ttl
  63      * @throws IOException
  64      */
  65     public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
  66             throws IOException, JdpException {
  67         this.addr = address;
  68         this.port = port;
  69 
  70         ProtocolFamily family = (address instanceof Inet6Address)
  71                 ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
  72 
  73         channel = DatagramChannel.open(family);
  74         channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
  75         channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
  76 
  77         // with srcAddress equal to null, this constructor do exactly the same as
  78         // if srcAddress is not passed
  79         if (srcAddress != null) {
  80             // User requests particular interface to bind to
  81             NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);













  82             try {
  83                 channel.bind(new InetSocketAddress(srcAddress, 0));
  84             } catch (UnsupportedAddressTypeException ex) {
  85                 throw new JdpException("Unable to bind to source address");
  86             }
  87             channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
  88         }
























  89     }
  90 
  91     /**
  92      * Create a new broadcaster
  93      *
  94      * @param address - multicast group address
  95      * @param port - udp port to use
  96      * @param ttl - packet ttl
  97      * @throws IOException
  98      */
  99     public JdpBroadcaster(InetAddress address, int port, int ttl)
 100             throws IOException, JdpException {
 101         this(address, null, port, ttl);
 102     }
 103 
 104     /**
 105      * Broadcast pre-built packet
 106      *
 107      * @param packet - instance of JdpPacket
 108      * @throws IOException


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.management.jdp;
  26 
  27 import java.io.IOException;
  28 import java.net.Inet6Address;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.net.NetworkInterface;
  32 import java.net.ProtocolFamily;
  33 import java.net.StandardProtocolFamily;
  34 import java.net.StandardSocketOptions;
  35 import java.nio.ByteBuffer;
  36 import java.nio.channels.DatagramChannel;
  37 import java.nio.channels.UnsupportedAddressTypeException;
  38 import java.util.Enumeration;
  39 
  40 /**
  41  * JdpBroadcaster is responsible for sending pre-built JDP packet across a Net
  42  *
  43  * <p> Multicast group address, port number and ttl have to be chosen on upper
  44  * level and passed to broadcaster constructor. Also it's possible to specify
  45  * source address to broadcast from. </p>
  46  *
  47  * <p>JdpBradcaster doesn't perform any validation on a supplied {@code port} and {@code ttl} because
  48  * the allowed values depend on an operating system setup</p>
  49  *
  50  */
  51 public final class JdpBroadcaster {
  52 
  53     private final InetAddress addr;
  54     private final int port;
  55     private final DatagramChannel channel;
  56 
  57     /**
  58      * Create a new broadcaster


  63      * @param ttl - packet ttl
  64      * @throws IOException
  65      */
  66     public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
  67             throws IOException, JdpException {
  68         this.addr = address;
  69         this.port = port;
  70 
  71         ProtocolFamily family = (address instanceof Inet6Address)
  72                 ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
  73 
  74         channel = DatagramChannel.open(family);
  75         channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
  76         channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
  77 
  78         // with srcAddress equal to null, this constructor do exactly the same as
  79         // if srcAddress is not passed
  80         if (srcAddress != null) {
  81             // User requests particular interface to bind to
  82             NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
  83 
  84             if (interf == null) {
  85                 throw new JdpException("Unable to get network interface for " + srcAddress.toString());
  86             }
  87 
  88             if (!interf.isUp()) {
  89                 throw new JdpException(interf.getName() + " is not up.");
  90             }
  91 
  92             if (!interf.supportsMulticast()) {
  93                 throw new JdpException(interf.getName() + " does not support multicast.");
  94             }
  95 
  96             try {
  97                 channel.bind(new InetSocketAddress(srcAddress, 0));
  98             } catch (UnsupportedAddressTypeException ex) {
  99                 throw new JdpException("Unable to bind to source address");
 100             }
 101             channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
 102         }
 103         else {
 104             Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
 105             boolean succeed = false;
 106 
 107             while (nics.hasMoreElements()) {
 108                 NetworkInterface nic = nics.nextElement();
 109 
 110                 if (nic.isUp() && nic.supportsMulticast()) {
 111                     try {
 112                         channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, nic);
 113                         succeed = true;
 114                     } catch (IOException ex) {
 115                         // pass
 116                     }
 117                 }
 118 
 119                 if (!succeed) {
 120                     throw new JdpException("Unable to bind to any interfaces.");
 121                 }
 122 
 123             }
 124 
 125         }
 126 
 127     }
 128 
 129     /**
 130      * Create a new broadcaster
 131      *
 132      * @param address - multicast group address
 133      * @param port - udp port to use
 134      * @param ttl - packet ttl
 135      * @throws IOException
 136      */
 137     public JdpBroadcaster(InetAddress address, int port, int ttl)
 138             throws IOException, JdpException {
 139         this(address, null, port, ttl);
 140     }
 141 
 142     /**
 143      * Broadcast pre-built packet
 144      *
 145      * @param packet - instance of JdpPacket
 146      * @throws IOException