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