1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  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
  58      *
  59      * @param address - multicast group address
  60      * @param srcAddress - address of interface we should use to broadcast.
  61      * @param port - udp port to use
  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
 109      */
 110     public void sendPacket(JdpPacket packet)
 111             throws IOException {
 112         byte[] data = packet.getPacketData();
 113         // Unlike allocate/put wrap don't need a flip afterward
 114         ByteBuffer b = ByteBuffer.wrap(data);
 115         channel.send(b, new InetSocketAddress(addr, port));
 116     }
 117 
 118     /**
 119      * Shutdown broadcaster and close underlying socket channel
 120      *
 121      * @throws IOException
 122      */
 123     public void shutdown() throws IOException {
 124         channel.close();
 125     }
 126 }