1 /*
   2  * Copyright (c) 2009, 2017, 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 com.sun.nio.sctp;
  26 
  27 import java.net.SocketAddress;
  28 import java.net.InetAddress;
  29 import java.io.IOException;
  30 import java.util.Set;
  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.spi.AbstractSelectableChannel;
  33 import java.nio.channels.spi.SelectorProvider;
  34 import java.nio.channels.ClosedChannelException;
  35 import java.nio.channels.NotYetBoundException;
  36 import java.nio.channels.SelectionKey;
  37 
  38 /**
  39  * A selectable channel for message-oriented SCTP sockets.
  40  *
  41  * <P> An SCTP multi channel supports many associations on a single socket.
  42  * An {@code SctpMultiChannel} is created by invoking the
  43  * {@link #open open} method of this class. A newly-created channel is open but
  44  * not yet bound. An attempt to invoke the {@link #receive receive} method of an
  45  * unbound channel will cause the {@link NotYetBoundException}
  46  * to be thrown. An attempt to invoke the {@link #send send} method of an
  47  * unbound channel will cause it to first invoke the {@link #bind bind} method.
  48  * The address(es) that the channel's socket is bound to can be retrieved by
  49  * calling {@link #getAllLocalAddresses getAllLocalAddresses}.
  50  *
  51  * <P> Messages may be sent and received without explicitly setting up an
  52  * association with the remote peer. The channel will implicitly setup
  53  * a new association whenever it sends or receives a message from a remote
  54  * peer if there is not already an association with that peer. Upon successful
  55  * association setup, an {@link AssociationChangeNotification
  56  * association changed} notification will be put to the SCTP stack with its
  57  * {@code event} parameter set to {@link
  58  * AssociationChangeNotification.AssocChangeEvent#COMM_UP
  59  * COMM_UP}. This notification can be received by invoking {@link #receive
  60  * receive}.
  61  *
  62  * <P> Socket options are configured using the
  63  * {@link #setOption(SctpSocketOption,Object,Association) setOption} method. An
  64  * {@code SctpMultiChannel} supports the following options:
  65  * <blockquote>
  66  * <table class="striped"><caption style="display:none">Socket options</caption>
  67  *   <tr>
  68  *     <th>Option Name</th>
  69  *     <th>Description</th>
  70  *   </tr>
  71  *   <tr>
  72  *     <td> {@link SctpStandardSocketOptions#SCTP_DISABLE_FRAGMENTS
  73  *                                          SCTP_DISABLE_FRAGMENTS} </td>
  74  *     <td> Enables or disables message fragmentation </td>
  75  *   </tr>
  76  *   <tr>
  77  *     <td> {@link SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE
  78  *                                          SCTP_EXPLICIT_COMPLETE} </td>
  79  *     <td> Enables or disables explicit message completion </td>
  80  *   </tr>
  81  *    <tr>
  82  *     <td> {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE
  83  *                                          SCTP_FRAGMENT_INTERLEAVE} </td>
  84  *     <td> Controls how the presentation of messages occur for the message
  85  *          receiver </td>
  86  *   </tr>
  87  *   <tr>
  88  *     <td> {@link SctpStandardSocketOptions#SCTP_INIT_MAXSTREAMS
  89  *                                          SCTP_INIT_MAXSTREAMS} </td>
  90  *     <td> The maximum number of streams requested by the local endpoint during
  91  *          association initialization </td>
  92  *   </tr>
  93  *   <tr>
  94  *     <td> {@link SctpStandardSocketOptions#SCTP_NODELAY SCTP_NODELAY} </td>
  95  *     <td> Enables or disable a Nagle-like algorithm </td>
  96  *   </tr>
  97  *   <tr>
  98  *     <td> {@link SctpStandardSocketOptions#SCTP_PRIMARY_ADDR
  99  *                                          SCTP_PRIMARY_ADDR} </td>
 100  *     <td> Requests that the local SCTP stack use the given peer address as the
 101  *          association primary </td>
 102  *   </tr>
 103  *   <tr>
 104  *     <td> {@link SctpStandardSocketOptions#SCTP_SET_PEER_PRIMARY_ADDR
 105  *                                          SCTP_SET_PEER_PRIMARY_ADDR} </td>
 106  *     <td> Requests that the peer mark the enclosed address as the association
 107  *          primary </td>
 108  *   </tr>
 109  *   <tr>
 110  *     <td> {@link SctpStandardSocketOptions#SO_SNDBUF
 111  *                                          SO_SNDBUF} </td>
 112  *     <td> The size of the socket send buffer </td>
 113  *   </tr>
 114  *   <tr>
 115  *     <td> {@link SctpStandardSocketOptions#SO_RCVBUF
 116  *                                          SO_RCVBUF} </td>
 117  *     <td> The size of the socket receive buffer </td>
 118  *   </tr>
 119  *   <tr>
 120  *     <td> {@link SctpStandardSocketOptions#SO_LINGER
 121  *                                          SO_LINGER} </td>
 122  *     <td> Linger on close if data is present (when configured in blocking mode
 123  *          only) </td>
 124  *   </tr>
 125  * </table>
 126  * </blockquote>
 127  * Additional (implementation specific) options may also be supported. The list
 128  * of options supported is obtained by invoking the {@link #supportedOptions()
 129  * supportedOptions} method.
 130  *
 131  * <p> SCTP multi channels are safe for use by multiple concurrent threads.
 132  * They support concurrent sending and receiving, though at most one thread may be
 133  * sending and at most one thread may be receiving at any given time.
 134  *
 135  * @since 1.7
 136  */
 137 public abstract class SctpMultiChannel
 138     extends AbstractSelectableChannel
 139 {
 140     /**
 141      * Initializes a new instance of this class.
 142      *
 143      * @param  provider
 144      *         The selector provider for this channel
 145      */
 146     protected SctpMultiChannel(SelectorProvider provider) {
 147         super(provider);
 148     }
 149 
 150     /**
 151      * Opens an SCTP multi channel.
 152      *
 153      * <P> The new channel is unbound.
 154      *
 155      * @return  A new SCTP multi channel
 156      *
 157      * @throws UnsupportedOperationException
 158      *         If the SCTP protocol is not supported
 159      *
 160      * @throws  IOException
 161      *          If an I/O error occurs
 162      */
 163     public static SctpMultiChannel open() throws
 164         IOException {
 165         return new sun.nio.ch.sctp.SctpMultiChannelImpl((SelectorProvider)null);
 166     }
 167 
 168     /**
 169      * Returns the open associations on this channel's socket.
 170      *
 171      * <P> Only associations whose {@link AssociationChangeNotification.AssocChangeEvent#COMM_UP
 172      * COMM_UP} association change event has been received are included
 173      * in the returned set of associations. Associations for which a
 174      * {@link AssociationChangeNotification.AssocChangeEvent#COMM_LOST COMM_LOST} or {@link
 175      * AssociationChangeNotification.AssocChangeEvent#SHUTDOWN SHUTDOWN} association change
 176      * event have been receive are removed from the set of associations.
 177      *
 178      * <P> The returned set of associations is a snapshot of the open
 179      * associations at the time that this method is invoked.
 180      *
 181      * @return  A {@code Set} containing the open associations, or an empty
 182      *          {@code Set} if there are none.
 183      *
 184      * @throws  ClosedChannelException
 185      *          If this channel is closed
 186      *
 187      * @throws  IOException
 188      *          If some other I/O error occurs
 189      */
 190     public abstract Set<Association> associations()
 191         throws IOException;
 192 
 193     /**
 194      * Binds the channel's socket to a local address and configures the socket
 195      * to listen for connections.
 196      *
 197      * <P> This method is used to establish a relationship between the socket
 198      * and the local address. Once a relationship is established then
 199      * the socket remains bound until the channel is closed. This relationship
 200      * may not necesssarily be with the address {@code local} as it may be removed
 201      * by {@link #unbindAddress unbindAddress}, but there will always be at least one local
 202      * address bound to the channel's socket once an invocation of this method
 203      * successfully completes.
 204      *
 205      * <P> Once the channel's socket has been successfully bound to a specific
 206      * address, that is not automatically assigned, more addresses
 207      * may be bound to it using {@link #bindAddress bindAddress}, or removed
 208      * using {@link #unbindAddress unbindAddress}.
 209      *
 210      * <P> The backlog parameter is the maximum number of pending connections on
 211      * the socket. Its exact semantics are implementation specific. An implementation
 212      * may impose an implementation specific maximum length or may choose to ignore
 213      * the parameter. If the backlog parameter has the value {@code 0}, or a negative
 214      * value, then an implementation specific default is used.
 215      *
 216      * @param  local
 217      *         The local address to bind the socket, or {@code null} to
 218      *         bind the socket to an automatically assigned socket address
 219      *
 220      * @param  backlog
 221      *         The maximum number of pending connections
 222      *
 223      * @return  This channel
 224      *
 225      * @throws  ClosedChannelException
 226      *          If this channel is closed
 227      *
 228      * @throws  java.nio.channels.AlreadyBoundException
 229      *          If this channel is already bound
 230      *
 231      * @throws  java.nio.channels.UnsupportedAddressTypeException
 232      *          If the type of the given address is not supported
 233      *
 234      * @throws  SecurityException
 235      *          If a security manager has been installed and its {@link
 236      *          java.lang.SecurityManager#checkListen(int) checkListen} method
 237      *          denies the operation
 238      *
 239      * @throws  IOException
 240      *          If some other I/O error occurs
 241      */
 242     public abstract SctpMultiChannel bind(SocketAddress local,
 243                                           int backlog)
 244         throws IOException;
 245 
 246     /**
 247      * Binds the channel's socket to a local address and configures the socket
 248      * to listen for connections.
 249      *
 250      * <P> This method works as if invoking it were equivalent to evaluating the
 251      * expression:
 252      * <blockquote><pre>
 253      * bind(local, 0);
 254      * </pre></blockquote>
 255      *
 256      * @param  local
 257      *         The local address to bind the socket, or {@code null} to
 258      *         bind the socket to an automatically assigned socket address
 259      *
 260      * @return  This channel
 261      *
 262      * @throws  ClosedChannelException
 263      *          If this channel is closed
 264      *
 265      * @throws  java.nio.channels.AlreadyBoundException
 266      *          If this channel is already bound
 267      *
 268      * @throws  java.nio.channels.UnsupportedAddressTypeException
 269      *          If the type of the given address is not supported
 270      *
 271      * @throws  SecurityException
 272      *          If a security manager has been installed and its {@link
 273      *          java.lang.SecurityManager#checkListen(int) checkListen} method
 274      *          denies the operation
 275      *
 276      * @throws  IOException
 277      *          If some other I/O error occurs
 278      */
 279     public final SctpMultiChannel bind(SocketAddress local)
 280         throws IOException {
 281         return bind(local, 0);
 282     }
 283 
 284     /**
 285      * Adds the given address to the bound addresses for the channel's
 286      * socket.
 287      *
 288      * <P> The given address must not be the {@link
 289      * java.net.InetAddress#isAnyLocalAddress wildcard} address.
 290      * The channel must be first bound using {@link #bind bind} before
 291      * invoking this method, otherwise {@link NotYetBoundException} is thrown.
 292      * The {@link #bind bind} method takes a {@code SocketAddress} as its
 293      * argument which typically contains a port number as well as an address.
 294      * Addresses subquently bound using this method are simply addresses as the
 295      * SCTP port number remains the same for the lifetime of the channel.
 296      *
 297      * <P> New associations setup after this method successfully completes
 298      * will be associated with the given address. Adding addresses to existing
 299      * associations is optional functionality. If the endpoint supports
 300      * dynamic address reconfiguration then it may send the appropriate message
 301      * to the peer to change the peers address lists.
 302      *
 303      * @param  address
 304      *         The address to add to the bound addresses for the socket
 305      *
 306      * @return  This channel
 307      *
 308      * @throws  ClosedChannelException
 309      *          If this channel is closed
 310      *
 311      * @throws  NotYetBoundException
 312      *          If this channel is not yet bound
 313      *
 314      * @throws  java.nio.channels.AlreadyBoundException
 315      *          If this channel is already bound to the given address
 316      *
 317      * @throws  IllegalArgumentException
 318      *          If address is {@code null} or the {@link
 319      *          java.net.InetAddress#isAnyLocalAddress wildcard} address
 320      *
 321      * @throws  IOException
 322      *          If some other I/O error occurs
 323      */
 324     public abstract SctpMultiChannel bindAddress(InetAddress address)
 325          throws IOException;
 326 
 327     /**
 328      * Removes the given address from the bound addresses for the channel's
 329      * socket.
 330      *
 331      * <P> The given address must not be the {@link
 332      * java.net.InetAddress#isAnyLocalAddress wildcard} address.
 333      * The channel must be first bound using {@link #bind bind} before
 334      * invoking this method, otherwise {@link NotYetBoundException} is thrown.
 335      *
 336      * <P> If this method is invoked on a channel that does
 337      * not have {@code address} as one of its bound addresses, or that has only
 338      * one local address bound to it, then this method throws
 339      * {@link IllegalUnbindException}.
 340      *
 341      * <P> The initial address that the channel's socket is bound to using
 342      * {@link #bind bind} may be removed from the bound addresses for the
 343      * channel's socket.
 344      *
 345      * <P> New associations setup after this method successfully completes
 346      * will not be associated with the given address. Removing addresses from
 347      * existing associations is optional functionality. If the endpoint supports
 348      * dynamic address reconfiguration then it may send the appropriate message
 349      * to the peer to change the peers address lists.
 350      *
 351      * @param  address
 352      *         The address to remove from the bound addresses for the socket
 353      *
 354      * @return  This channel
 355      *
 356      * @throws  ClosedChannelException
 357      *          If this channel is closed
 358      *
 359      * @throws  NotYetBoundException
 360      *          If this channel is not yet bound
 361      *
 362      * @throws  IllegalUnbindException
 363      *          {@code address} is not bound to the channel's socket, or the
 364      *          channel has only one address  bound to it
 365      *
 366      * @throws  IllegalArgumentException
 367      *          If address is {@code null} or the {@link
 368      *          java.net.InetAddress#isAnyLocalAddress wildcard} address
 369      *
 370      * @throws  IOException
 371      *          If some other I/O error occurs
 372      */
 373     public abstract SctpMultiChannel unbindAddress(InetAddress address)
 374          throws IOException;
 375 
 376     /**
 377      * Returns all of the socket addresses to which this channel's socket is
 378      * bound.
 379      *
 380      * @return  All the socket addresses that this channel's socket is
 381      *          bound to, or an empty {@code Set} if the channel's socket is not
 382      *          bound
 383      *
 384      * @throws  ClosedChannelException
 385      *          If the channel is closed
 386      *
 387      * @throws  IOException
 388      *          If an I/O error occurs
 389      */
 390     public abstract Set<SocketAddress> getAllLocalAddresses()
 391         throws IOException;
 392 
 393     /**
 394      * Returns all of the remote addresses to which the given association on
 395      * this channel's socket is connected.
 396      *
 397      * @param  association
 398      *         The association
 399      *
 400      * @return  All of the remote addresses for the given association, or
 401      *          an empty {@code Set} if the association has been shutdown
 402      *
 403      * @throws  ClosedChannelException
 404      *          If the channel is closed
 405      *
 406      * @throws  IOException
 407      *          If an I/O error occurs
 408      */
 409     public abstract Set<SocketAddress> getRemoteAddresses(Association association)
 410         throws IOException;
 411 
 412     /**
 413      * Shutdown an association without closing the channel.
 414      *
 415      * @param  association
 416      *         The association to shutdown
 417      *
 418      * @return  This channel
 419      *
 420      * @throws  ClosedChannelException
 421      *          If this channel is closed
 422      *
 423      * @throws  IOException
 424      *          If some other I/O error occurs
 425      */
 426     public abstract SctpMultiChannel shutdown(Association association)
 427             throws IOException;
 428 
 429     /**
 430      * Returns the value of a socket option.
 431      *
 432      * <P> Note that some options are retrieved on the channel's socket,
 433      * therefore the {@code association} parameter is not applicable and will be
 434      * ignored if given. However, if the option is association specific then the
 435      * association must be given.
 436      *
 437      * @param  <T>
 438      *         The type of the socket option value
 439      *
 440      * @param  name
 441      *         The socket option
 442      *
 443      * @param  association
 444      *         The association whose option should be retrieved, or {@code null}
 445      *         if this option should be retrieved at the channel's socket level.
 446      *
 447      * @return  The value of the socket option. A value of {@code null} may be
 448      *          a valid value for some socket options.
 449      *
 450      * @throws  UnsupportedOperationException
 451      *          If the socket option is not supported by this channel
 452      *
 453      * @throws  ClosedChannelException
 454      *          If this channel is closed
 455      *
 456      * @throws  IOException
 457      *          If an I/O error occurs
 458      *
 459      * @see SctpStandardSocketOptions
 460      */
 461     public abstract <T> T getOption(SctpSocketOption<T> name,
 462                                     Association association)
 463         throws IOException;
 464 
 465     /**
 466      * Sets the value of a socket option.
 467      *
 468      * <P> Note that some options are retrieved on the channel's socket,
 469      * therefore the {@code association} parameter is not applicable and will be
 470      * ignored if given. However, if the option is association specific then the
 471      * association must be given.
 472      *
 473      * @param   <T>
 474      *          The type of the socket option value
 475      *
 476      * @param   name
 477      *          The socket option
 478      *
 479      * @param  association
 480      *         The association whose option should be set, or {@code null}
 481      *         if this option should be set at the channel's socket level.
 482      *
 483      * @param   value
 484      *          The value of the socket option. A value of {@code null} may be
 485      *          a valid value for some socket options.
 486      *
 487      * @return  This channel
 488      *
 489      * @throws  UnsupportedOperationException
 490      *          If the socket option is not supported by this channel
 491      *
 492      * @throws  IllegalArgumentException
 493      *          If the value is not a valid value for this socket option
 494      *
 495      * @throws  ClosedChannelException
 496      *          If this channel is closed
 497      *
 498      * @throws  IOException
 499      *          If an I/O error occurs
 500      *
 501      * @see SctpStandardSocketOptions
 502      */
 503     public abstract <T> SctpMultiChannel setOption(SctpSocketOption<T> name,
 504                                                    T value,
 505                                                    Association association)
 506          throws IOException;
 507 
 508      /**
 509      * Returns a set of the socket options supported by this channel.
 510      *
 511      * <P> This method will continue to return the set of options even after the
 512      * channel has been closed.
 513      *
 514      * @return  A set of the socket options supported by this channel
 515      */
 516     public abstract Set<SctpSocketOption<?>> supportedOptions();
 517 
 518     /**
 519      * Returns an operation set identifying this channel's supported operations.
 520      *
 521      * <P> SCTP multi channels support reading, and writing, so this
 522      * method returns
 523      * {@code (}{@link SelectionKey#OP_READ} {@code |}&nbsp;{@link
 524      * SelectionKey#OP_WRITE}{@code )}.  </p>
 525      *
 526      * @return  The valid-operation set
 527      */
 528     @Override
 529     public final int validOps() {
 530         return (SelectionKey.OP_READ |
 531                 SelectionKey.OP_WRITE );
 532     }
 533 
 534     /**
 535      * Receives a message and/or handles a notification via this channel.
 536      *
 537      * <P> If a message or notification is immediately available, or if this
 538      * channel is in blocking mode and one eventually becomes available, then
 539      * the message or notification is returned or handled, respectively. If this
 540      * channel is in non-blocking mode and a message or notification is not
 541      * immediately available then this method immediately returns {@code null}.
 542      *
 543      * <P> If this method receives a message it is copied into the given byte
 544      * buffer and an {@link MessageInfo} is returned.
 545      * The message is transferred into the given byte buffer starting at its
 546      * current position and the buffers position is incremented by the number of
 547      * bytes read. If there are fewer bytes remaining in the buffer than are
 548      * required to hold the message, or the underlying input buffer does not
 549      * contain the complete message, then an invocation of {@link
 550      * MessageInfo#isComplete isComplete} on the returned {@code
 551      * MessageInfo} will return {@code false}, and more invocations of this
 552      * method will be necessary to completely consume the messgae. Only
 553      * one message at a time will be partially delivered in any stream. The
 554      * socket option {@link SctpStandardSocketOptions#SCTP_FRAGMENT_INTERLEAVE
 555      * SCTP_FRAGMENT_INTERLEAVE} controls various aspects of what interlacing of
 556      * messages occurs.
 557      *
 558      * <P> If this method receives a notification then the appropriate method of
 559      * the given handler, if there is one, is invoked. If the handler returns {@link
 560      * HandlerResult#CONTINUE CONTINUE} then this method will try to receive another
 561      * message/notification, otherwise, if {@link HandlerResult#RETURN RETURN} is returned
 562      * this method will return {@code null}. If an uncaught exception is thrown by the
 563      * handler it will be propagated up the stack through this method.
 564      *
 565      * <P> If a security manager has been installed then for each new association
 566      * setup this method verifies that the associations source address and port
 567      * number are permitted by the security manager's {@link
 568      * java.lang.SecurityManager#checkAccept(String,int) checkAccept} method.
 569      *
 570      * <P> This method may be invoked at any time. If another thread has
 571      * already initiated a receive operation upon this channel, then an
 572      * invocation of this method will block until the first operation is
 573      * complete. The given handler is invoked without holding any locks used
 574      * to enforce the above synchronization policy, that way handlers
 575      * will not stall other threads from receiving. A handler should not invoke
 576      * the {@code receive} method of this channel, if it does an
 577      * {@link IllegalReceiveException} will be thrown.
 578      *
 579      * @param  <T>
 580      *         The type of the attachment
 581      *
 582      * @param  buffer
 583      *         The buffer into which bytes are to be transferred
 584      *
 585      * @param  attachment
 586      *         The object to attach to the receive operation; can be
 587      *         {@code null}
 588      *
 589      * @param  handler
 590      *         A handler to handle notifications from the SCTP stack, or
 591      *         {@code null} to ignore any notifications.
 592      *
 593      * @return  The {@code MessageInfo}, {@code null} if this channel is in
 594      *          non-blocking mode and no messages are immediately available or
 595      *          the notification handler returns {@code RETURN} after handling
 596      *          a notification
 597      *
 598      * @throws  java.nio.channels.ClosedChannelException
 599      *          If this channel is closed
 600      *
 601      * @throws  java.nio.channels.AsynchronousCloseException
 602      *          If another thread closes this channel
 603      *          while the read operation is in progress
 604      *
 605      * @throws  java.nio.channels.ClosedByInterruptException
 606      *          If another thread interrupts the current thread
 607      *          while the read operation is in progress, thereby
 608      *          closing the channel and setting the current thread's
 609      *          interrupt status
 610      *
 611      * @throws  NotYetBoundException
 612      *          If this channel is not yet bound
 613      *
 614      * @throws  IllegalReceiveException
 615      *          If the given handler invokes the {@code receive} method of this
 616      *          channel
 617      *
 618      * @throws  SecurityException
 619      *          If a security manager has been installed and it does not permit
 620      *          new associations to be accepted from the message's sender
 621      *
 622      * @throws  IOException
 623      *          If some other I/O error occurs
 624      */
 625     public abstract <T> MessageInfo receive(ByteBuffer buffer,
 626                                             T attachment,
 627                                             NotificationHandler<T> handler)
 628         throws IOException;
 629 
 630     /**
 631      * Sends a message via this channel.
 632      *
 633      * <P> If this channel is unbound then this method will invoke {@link
 634      * #bind(SocketAddress, int) bind(null, 0)} before sending any data.
 635      *
 636      * <P> If there is no association existing between this channel's socket
 637      * and the intended receiver, identified by the address in the given messageInfo, then one
 638      * will be automatically setup to the intended receiver. This is considered
 639      * to be Implicit Association Setup. Upon successful association setup, an
 640      * {@link AssociationChangeNotification association changed}
 641      * notification will be put to the SCTP stack with its {@code event} parameter set
 642      * to {@link AssociationChangeNotification.AssocChangeEvent#COMM_UP COMM_UP}
 643      * . This notification can be received by invoking {@link #receive
 644      * receive}.
 645      *
 646      * <P> If this channel is in blocking mode, there is sufficient room in the
 647      * underlying output buffer, then the remaining bytes in the given byte
 648      * buffer are transmitted as a single message. Sending a message
 649      * is atomic unless explicit message completion {@link
 650      * SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE SCTP_EXPLICIT_COMPLETE}
 651      * socket option is enabled on this channel's socket.
 652      *
 653      * <P> If this channel is in non-blocking mode, there is sufficient room
 654      * in the underlying output buffer, and an implicit association setup is
 655      * required, then the remaining bytes in the given byte buffer are
 656      * transmitted as a single message, subject to {@link
 657      * SctpStandardSocketOptions#SCTP_EXPLICIT_COMPLETE SCTP_EXPLICIT_COMPLETE}.
 658      * If for any reason the message cannot
 659      * be delivered an {@link AssociationChangeNotification association
 660      * changed} notification is put on the SCTP stack with its {@code event} parameter set
 661      * to {@link AssociationChangeNotification.AssocChangeEvent#CANT_START CANT_START}.
 662      *
 663      * <P> The message is transferred from the byte buffer as if by a regular
 664      * {@link java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer)
 665      * write} operation.
 666      *
 667      * <P> If a security manager has been installed then for each new association
 668      * setup this method verifies that the given remote peers address and port
 669      * number are permitted by the security manager's {@link
 670      * java.lang.SecurityManager#checkConnect(String,int) checkConnect} method.
 671      *
 672      * <P> This method may be invoked at any time. If another thread has already
 673      * initiated a send operation upon this channel, then an invocation of
 674      * this method will block until the first operation is complete.
 675      *
 676      * @param  buffer
 677      *         The buffer containing the message to be sent
 678      *
 679      * @param  messageInfo
 680      *         Ancillary data about the message to be sent
 681      *
 682      * @return  The number of bytes sent, which will be either the number of
 683      *          bytes that were remaining in the messages buffer when this method
 684      *          was invoked or, if this channel is non-blocking, may be zero if
 685      *          there was insufficient room for the message in the underlying
 686      *          output buffer
 687      *
 688      * @throws  InvalidStreamException
 689      *          If {@code streamNumber} is negative, or if an association already
 690      *          exists and {@code streamNumber} is greater than the maximum number
 691      *          of outgoing streams
 692      *
 693      * @throws  java.nio.channels.ClosedChannelException
 694      *          If this channel is closed
 695      *
 696      * @throws  java.nio.channels.AsynchronousCloseException
 697      *          If another thread closes this channel
 698      *          while the read operation is in progress
 699      *
 700      * @throws  java.nio.channels.ClosedByInterruptException
 701      *          If another thread interrupts the current thread
 702      *          while the read operation is in progress, thereby
 703      *          closing the channel and setting the current thread's
 704      *          interrupt status
 705      *
 706      * @throws  SecurityException
 707      *          If a security manager has been installed and it does not permit
 708      *          new associations to be setup with the messages's address
 709      *
 710      * @throws  IOException
 711      *          If some other I/O error occurs
 712      */
 713     public abstract int send(ByteBuffer buffer, MessageInfo messageInfo)
 714         throws IOException;
 715 
 716     /**
 717      * Branches off an association.
 718      *
 719      * <P> An application can invoke this method to branch off an association
 720      * into a separate channel. The new bound and connected {@link SctpChannel}
 721      * will be created for the association. The branched off association will no
 722      * longer be part of this channel.
 723      *
 724      * <P> This is particularly useful when, for instance, the application
 725      * wishes to have a number of sporadic message senders/receivers remain
 726      * under the original SCTP multi channel but branch off those
 727      * associations carrying high volume data traffic into their own
 728      * separate SCTP channels.
 729      *
 730      * @param  association
 731      *         The association to branch off
 732      *
 733      * @return  The {@code SctpChannel}
 734      *
 735      * @throws  java.nio.channels.ClosedChannelException
 736      *          If this channel is closed
 737      *
 738      * @throws  IOException
 739      *          If some other I/O error occurs
 740      */
 741     public abstract SctpChannel branch(Association association)
 742         throws IOException;
 743 }