| 1 | /* MulticastSocket.java -- Class for using multicast sockets
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
|
|---|
| 3 | Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of GNU Classpath.
|
|---|
| 6 |
|
|---|
| 7 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 10 | any later version.
|
|---|
| 11 |
|
|---|
| 12 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 20 | 02111-1307 USA.
|
|---|
| 21 |
|
|---|
| 22 | Linking this library statically or dynamically with other modules is
|
|---|
| 23 | making a combined work based on this library. Thus, the terms and
|
|---|
| 24 | conditions of the GNU General Public License cover the whole
|
|---|
| 25 | combination.
|
|---|
| 26 |
|
|---|
| 27 | As a special exception, the copyright holders of this library give you
|
|---|
| 28 | permission to link this library with independent modules to produce an
|
|---|
| 29 | executable, regardless of the license terms of these independent
|
|---|
| 30 | modules, and to copy and distribute the resulting executable under
|
|---|
| 31 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 32 | independent module, the terms and conditions of the license of that
|
|---|
| 33 | module. An independent module is a module which is not derived from
|
|---|
| 34 | or based on this library. If you modify this library, you may extend
|
|---|
| 35 | this exception to your version of the library, but you are not
|
|---|
| 36 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 37 | exception statement from your version. */
|
|---|
| 38 |
|
|---|
| 39 | package java.net;
|
|---|
| 40 |
|
|---|
| 41 | import java.io.IOException;
|
|---|
| 42 | import java.util.Enumeration;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 46 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 47 | * Status: Believed complete and correct.
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * This class models a multicast UDP socket. A multicast address is a
|
|---|
| 52 | * class D internet address (one whose most significant bits are 1110).
|
|---|
| 53 | * A multicast group consists of a multicast address and a well known
|
|---|
| 54 | * port number. All members of the group listening on that address and
|
|---|
| 55 | * port will receive all the broadcasts to the group.
|
|---|
| 56 | * <p>
|
|---|
| 57 | * Please note that applets are not allowed to use multicast sockets
|
|---|
| 58 | *
|
|---|
| 59 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 60 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 61 | * Status: Believed complete and correct.
|
|---|
| 62 | *
|
|---|
| 63 | * @author Warren Levy <[email protected]>
|
|---|
| 64 | * @author Aaron M. Renn ([email protected]) (Documentation comments)
|
|---|
| 65 | * @since 1.1
|
|---|
| 66 | * @date May 18, 1999.
|
|---|
| 67 | */
|
|---|
| 68 | public class MulticastSocket extends DatagramSocket
|
|---|
| 69 | {
|
|---|
| 70 | // FIXME: the local addr bound to the multicast socket can be reused;
|
|---|
| 71 | // unlike unicast sockets. It binds to any available network interface.
|
|---|
| 72 | // See p.1159 JCL book.
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Create a MulticastSocket that this not bound to any address
|
|---|
| 76 | *
|
|---|
| 77 | * @exception IOException If an error occurs
|
|---|
| 78 | * @exception SecurityException If a security manager exists and its
|
|---|
| 79 | * checkListen method doesn't allow the operation
|
|---|
| 80 | */
|
|---|
| 81 | public MulticastSocket() throws IOException
|
|---|
| 82 | {
|
|---|
| 83 | super(0, null);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * Create a multicast socket bound to the specified port
|
|---|
| 88 | *
|
|---|
| 89 | * @param port The port to bind to
|
|---|
| 90 | *
|
|---|
| 91 | * @exception IOException If an error occurs
|
|---|
| 92 | * @exception SecurityException If a security manager exists and its
|
|---|
| 93 | * checkListen method doesn't allow the operation
|
|---|
| 94 | */
|
|---|
| 95 | public MulticastSocket(int port) throws IOException
|
|---|
| 96 | {
|
|---|
| 97 | super(port, null);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Create a multicast socket bound to the specified SocketAddress.
|
|---|
| 102 | *
|
|---|
| 103 | * @param address The SocketAddress the multicast socket will be bound to
|
|---|
| 104 | *
|
|---|
| 105 | * @exception IOException If an error occurs
|
|---|
| 106 | * @exception SecurityException If a security manager exists and its
|
|---|
| 107 | * checkListen method doesn't allow the operation
|
|---|
| 108 | *
|
|---|
| 109 | * @since 1.4
|
|---|
| 110 | */
|
|---|
| 111 | public MulticastSocket(SocketAddress address) throws IOException
|
|---|
| 112 | {
|
|---|
| 113 | super(address);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Returns the interface being used for multicast packets
|
|---|
| 118 | *
|
|---|
| 119 | * @return The multicast interface
|
|---|
| 120 | *
|
|---|
| 121 | * @exception SocketException If an error occurs
|
|---|
| 122 | */
|
|---|
| 123 | public InetAddress getInterface() throws SocketException
|
|---|
| 124 | {
|
|---|
| 125 | return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Returns the current value of the "Time to Live" option. This is the
|
|---|
| 130 | * number of hops a packet can make before it "expires". This method id
|
|---|
| 131 | * deprecated. Use <code>getTimeToLive</code> instead.
|
|---|
| 132 | *
|
|---|
| 133 | * @return The TTL value
|
|---|
| 134 | *
|
|---|
| 135 | * @exception IOException If an error occurs
|
|---|
| 136 | *
|
|---|
| 137 | * @deprecated 1.2 Replaced by getTimeToLive()
|
|---|
| 138 | *
|
|---|
| 139 | * @see Multicastsocket:getTimeToLive
|
|---|
| 140 | */
|
|---|
| 141 | public byte getTTL() throws IOException
|
|---|
| 142 | {
|
|---|
| 143 | // Use getTTL here rather than getTimeToLive in case we're using an impl
|
|---|
| 144 | // other than the default PlainDatagramSocketImpl and it doesn't have
|
|---|
| 145 | // getTimeToLive yet.
|
|---|
| 146 | return impl.getTTL();
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Returns the current value of the "Time to Live" option. This is the
|
|---|
| 151 | * number of hops a packet can make before it "expires".
|
|---|
| 152 | *
|
|---|
| 153 | * @return The TTL value
|
|---|
| 154 | *
|
|---|
| 155 | * @exception IOException If an error occurs
|
|---|
| 156 | *
|
|---|
| 157 | * @since 1.2
|
|---|
| 158 | */
|
|---|
| 159 | public int getTimeToLive() throws IOException
|
|---|
| 160 | {
|
|---|
| 161 | return impl.getTimeToLive();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 165 | * Sets the interface to use for sending multicast packets.
|
|---|
| 166 | *
|
|---|
| 167 | * @param addr The new interface to use.
|
|---|
| 168 | *
|
|---|
| 169 | * @exception SocketException If an error occurs.
|
|---|
| 170 | *
|
|---|
| 171 | * @since 1.4
|
|---|
| 172 | */
|
|---|
| 173 | public void setInterface(InetAddress addr) throws SocketException
|
|---|
| 174 | {
|
|---|
| 175 | impl.setOption(SocketOptions.IP_MULTICAST_IF, addr);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /**
|
|---|
| 179 | * Sets the local network interface used to send multicast messages
|
|---|
| 180 | *
|
|---|
| 181 | * @param netIF The local network interface used to send multicast messages
|
|---|
| 182 | *
|
|---|
| 183 | * @exception SocketException If an error occurs
|
|---|
| 184 | *
|
|---|
| 185 | * @see MulticastSocket:getNetworkInterface
|
|---|
| 186 | *
|
|---|
| 187 | * @since 1.4
|
|---|
| 188 | */
|
|---|
| 189 | public void setNetworkInterface(NetworkInterface netIf)
|
|---|
| 190 | throws SocketException
|
|---|
| 191 | {
|
|---|
| 192 | if (impl == null)
|
|---|
| 193 | throw new SocketException (
|
|---|
| 194 | "MulticastSocket: Cant access socket implementation");
|
|---|
| 195 |
|
|---|
| 196 | Enumeration e = netIf.getInetAddresses ();
|
|---|
| 197 |
|
|---|
| 198 | if (!e.hasMoreElements ())
|
|---|
| 199 | throw new SocketException ("MulticastSocket: Error");
|
|---|
| 200 |
|
|---|
| 201 | InetAddress address = (InetAddress) e.nextElement ();
|
|---|
| 202 | impl.setOption (SocketOptions.IP_MULTICAST_IF, address);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Gets the local network interface which is used to send multicast messages
|
|---|
| 207 | *
|
|---|
| 208 | * @return The local network interface to send multicast messages
|
|---|
| 209 | *
|
|---|
| 210 | * @exception SocketException If an error occurs
|
|---|
| 211 | *
|
|---|
| 212 | * @see MulticastSocket:setNetworkInterface
|
|---|
| 213 | *
|
|---|
| 214 | * @since 1.4
|
|---|
| 215 | */
|
|---|
| 216 | public NetworkInterface getNetworkInterface()
|
|---|
| 217 | throws SocketException
|
|---|
| 218 | {
|
|---|
| 219 | if (impl == null)
|
|---|
| 220 | throw new SocketException (
|
|---|
| 221 | "MulticastSocket: Cant access socket implementation");
|
|---|
| 222 |
|
|---|
| 223 | InetAddress address =
|
|---|
| 224 | (InetAddress) impl.getOption (SocketOptions.IP_MULTICAST_IF);
|
|---|
| 225 | NetworkInterface netIf = NetworkInterface.getByInetAddress (address);
|
|---|
| 226 |
|
|---|
| 227 | return netIf;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /**
|
|---|
| 231 | * Disable/Enable local loopback of multicast packets. The option is used by
|
|---|
| 232 | * the platform's networking code as a hint for setting whether multicast
|
|---|
| 233 | * data will be looped back to the local socket.
|
|---|
| 234 | *
|
|---|
| 235 | * Because this option is a hint, applications that want to verify what
|
|---|
| 236 | * loopback mode is set to should call #getLoopbackMode
|
|---|
| 237 | *
|
|---|
| 238 | * @param disable True to disable loopback mode
|
|---|
| 239 | *
|
|---|
| 240 | * @exception SocketException If an error occurs
|
|---|
| 241 | *
|
|---|
| 242 | * @since 1.4
|
|---|
| 243 | */
|
|---|
| 244 | public void setLoopbackMode(boolean disable) throws SocketException
|
|---|
| 245 | {
|
|---|
| 246 | if (impl == null)
|
|---|
| 247 | throw new SocketException (
|
|---|
| 248 | "MulticastSocket: Cant access socket implementation");
|
|---|
| 249 |
|
|---|
| 250 | impl.setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable));
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Checks if local loopback mode is enabled or not
|
|---|
| 255 | *
|
|---|
| 256 | * @exception SocketException If an error occurs
|
|---|
| 257 | *
|
|---|
| 258 | * @since 1.4
|
|---|
| 259 | */
|
|---|
| 260 | public boolean getLoopbackMode() throws SocketException
|
|---|
| 261 | {
|
|---|
| 262 | Object obj = impl.getOption (SocketOptions.IP_MULTICAST_LOOP);
|
|---|
| 263 |
|
|---|
| 264 | if (obj instanceof Boolean)
|
|---|
| 265 | return ((Boolean) obj).booleanValue ();
|
|---|
| 266 | else
|
|---|
| 267 | throw new SocketException ("Unexpected type");
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /**
|
|---|
| 271 | * Sets the "Time to Live" value for a socket. The value must be between
|
|---|
| 272 | * 1 and 255.
|
|---|
| 273 | *
|
|---|
| 274 | * @param ttl The new TTL value
|
|---|
| 275 | *
|
|---|
| 276 | * @exception IOException If an error occurs
|
|---|
| 277 | *
|
|---|
| 278 | * @deprecated 1.2 Replaced by <code>setTimeToLive</code>
|
|---|
| 279 | *
|
|---|
| 280 | * @see MulticastSocket:setTimeToLive
|
|---|
| 281 | */
|
|---|
| 282 | public void setTTL(byte ttl) throws IOException
|
|---|
| 283 | {
|
|---|
| 284 | // Use setTTL here rather than setTimeToLive in case we're using an impl
|
|---|
| 285 | // other than the default PlainDatagramSocketImpl and it doesn't have
|
|---|
| 286 | // setTimeToLive yet.
|
|---|
| 287 | impl.setTTL(ttl);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /**
|
|---|
| 291 | * Sets the "Time to Live" value for a socket. The value must be between
|
|---|
| 292 | * 1 and 255.
|
|---|
| 293 | *
|
|---|
| 294 | * @param ttl The new TTL value
|
|---|
| 295 | *
|
|---|
| 296 | * @exception IOException If an error occurs
|
|---|
| 297 | *
|
|---|
| 298 | * @since 1.2
|
|---|
| 299 | */
|
|---|
| 300 | public void setTimeToLive(int ttl) throws IOException
|
|---|
| 301 | {
|
|---|
| 302 | if (ttl <= 0 || ttl > 255)
|
|---|
| 303 | throw new IllegalArgumentException("Invalid ttl: " + ttl);
|
|---|
| 304 |
|
|---|
| 305 | impl.setTimeToLive(ttl);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Joins the specified mulitcast group.
|
|---|
| 310 | *
|
|---|
| 311 | * @param addr The address of the group to join
|
|---|
| 312 | *
|
|---|
| 313 | * @exception IOException If an error occurs
|
|---|
| 314 | * @exception SecurityException If a security manager exists and its
|
|---|
| 315 | * checkMulticast method doesn't allow the operation
|
|---|
| 316 | */
|
|---|
| 317 | public void joinGroup(InetAddress mcastaddr) throws IOException
|
|---|
| 318 | {
|
|---|
| 319 | if (! mcastaddr.isMulticastAddress())
|
|---|
| 320 | throw new IOException("Not a Multicast address");
|
|---|
| 321 |
|
|---|
| 322 | SecurityManager s = System.getSecurityManager();
|
|---|
| 323 | if (s != null)
|
|---|
| 324 | s.checkMulticast(mcastaddr);
|
|---|
| 325 |
|
|---|
| 326 | impl.join(mcastaddr);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /**
|
|---|
| 330 | * Leaves the specified multicast group
|
|---|
| 331 | *
|
|---|
| 332 | * @param addr The address of the group to leave
|
|---|
| 333 | *
|
|---|
| 334 | * @exception IOException If an error occurs
|
|---|
| 335 | * @exception SecurityException If a security manager exists and its
|
|---|
| 336 | * checkMulticast method doesn't allow the operation
|
|---|
| 337 | */
|
|---|
| 338 | public void leaveGroup(InetAddress mcastaddr) throws IOException
|
|---|
| 339 | {
|
|---|
| 340 | if (! mcastaddr.isMulticastAddress())
|
|---|
| 341 | throw new IOException("Not a Multicast address");
|
|---|
| 342 |
|
|---|
| 343 | SecurityManager s = System.getSecurityManager();
|
|---|
| 344 | if (s != null)
|
|---|
| 345 | s.checkMulticast(mcastaddr);
|
|---|
| 346 |
|
|---|
| 347 | impl.leave(mcastaddr);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /**
|
|---|
| 351 | * Joins the specified mulitcast group on a specified interface.
|
|---|
| 352 | *
|
|---|
| 353 | * @param mcastaddr The multicast address to join
|
|---|
| 354 | * @param netIf The local network interface to receive the multicast
|
|---|
| 355 | * messages on or null to defer the interface set by #setInterface or
|
|---|
| 356 | * #setNetworkInterface
|
|---|
| 357 | *
|
|---|
| 358 | * @exception IOException If an error occurs
|
|---|
| 359 | * @exception IllegalArgumentException If address type is not supported
|
|---|
| 360 | * @exception SecurityException If a security manager exists and its
|
|---|
| 361 | * checkMulticast method doesn't allow the operation
|
|---|
| 362 | *
|
|---|
| 363 | * @see MulticastSocket:setInterface
|
|---|
| 364 | * @see MulticastSocket:setNetworkInterface
|
|---|
| 365 | *
|
|---|
| 366 | * @since 1.4
|
|---|
| 367 | */
|
|---|
| 368 | public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
|
|---|
| 369 | throws IOException
|
|---|
| 370 | {
|
|---|
| 371 | if (! (mcastaddr instanceof InetSocketAddress))
|
|---|
| 372 | throw new IllegalArgumentException ("SocketAddress type not supported");
|
|---|
| 373 |
|
|---|
| 374 | InetSocketAddress tmp = (InetSocketAddress) mcastaddr;
|
|---|
| 375 |
|
|---|
| 376 | if (! tmp.getAddress ().isMulticastAddress ())
|
|---|
| 377 | throw new IOException ("Not a Multicast address");
|
|---|
| 378 |
|
|---|
| 379 | SecurityManager s = System.getSecurityManager ();
|
|---|
| 380 | if (s != null)
|
|---|
| 381 | s.checkMulticast (tmp.getAddress ());
|
|---|
| 382 |
|
|---|
| 383 | impl.joinGroup (mcastaddr, netIf);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| 387 | * Leaves the specified mulitcast group on a specified interface.
|
|---|
| 388 | *
|
|---|
| 389 | * @param mcastaddr The multicast address to leave
|
|---|
| 390 | * @param netIf The local networki interface or null to defer to the
|
|---|
| 391 | * interface set by setInterface or setNetworkInterface
|
|---|
| 392 | *
|
|---|
| 393 | * @exception IOException If an error occurs
|
|---|
| 394 | * @exception IllegalArgumentException If address type is not supported
|
|---|
| 395 | * @exception SecurityException If a security manager exists and its
|
|---|
| 396 | * checkMulticast method doesn't allow the operation
|
|---|
| 397 | *
|
|---|
| 398 | * @see MulticastSocket:setInterface
|
|---|
| 399 | * @see MulticastSocket:setNetworkInterface
|
|---|
| 400 | *
|
|---|
| 401 | * @since 1.4
|
|---|
| 402 | */
|
|---|
| 403 | public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
|
|---|
| 404 | throws IOException
|
|---|
| 405 | {
|
|---|
| 406 | InetSocketAddress tmp = (InetSocketAddress) mcastaddr;
|
|---|
| 407 |
|
|---|
| 408 | if (! tmp.getAddress ().isMulticastAddress ())
|
|---|
| 409 | throw new IOException ("Not a Multicast address");
|
|---|
| 410 |
|
|---|
| 411 | SecurityManager s = System.getSecurityManager ();
|
|---|
| 412 | if (s != null)
|
|---|
| 413 | s.checkMulticast (tmp.getAddress ());
|
|---|
| 414 |
|
|---|
| 415 | impl.leaveGroup (mcastaddr, netIf);
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | /**
|
|---|
| 419 | * Sends a packet of data to a multicast address with a TTL that is
|
|---|
| 420 | * different from the default TTL on this socket. The default TTL for
|
|---|
| 421 | * the socket is not changed.
|
|---|
| 422 | *
|
|---|
| 423 | * @param packet The packet of data to send
|
|---|
| 424 | * @param ttl The TTL for this packet
|
|---|
| 425 | *
|
|---|
| 426 | * @exception IOException If an error occurs
|
|---|
| 427 | * @exception SecurityException If a security manager exists and its
|
|---|
| 428 | * checkConnect or checkMulticast method doesn't allow the operation
|
|---|
| 429 | */
|
|---|
| 430 | public synchronized void send(DatagramPacket p, byte ttl) throws IOException
|
|---|
| 431 | {
|
|---|
| 432 | SecurityManager s = System.getSecurityManager();
|
|---|
| 433 | if (s != null)
|
|---|
| 434 | {
|
|---|
| 435 | InetAddress addr = p.getAddress();
|
|---|
| 436 | if (addr.isMulticastAddress())
|
|---|
| 437 | s.checkMulticast(addr, ttl);
|
|---|
| 438 | else
|
|---|
| 439 | s.checkConnect(addr.getHostAddress(), p.getPort());
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | int oldttl = impl.getTimeToLive();
|
|---|
| 443 | impl.setTimeToLive(((int) ttl) & 0xFF);
|
|---|
| 444 | impl.send(p);
|
|---|
| 445 | impl.setTimeToLive(oldttl);
|
|---|
| 446 | }
|
|---|
| 447 | } // class MulticastSocket
|
|---|