src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpMultiChannelImpl.java@3317bb8137f4
child 49417 1d3139252c1c
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22604
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     4
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    10
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    15
 * accompanied this code).
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    16
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4677
diff changeset
    23
 * questions.
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
 */
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    25
package sun.nio.ch.sctp;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    26
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    27
import java.net.InetAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
import java.net.SocketAddress;
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
    29
import java.net.SocketException;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.FileDescriptor;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.Collections;
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
    34
import java.util.Map.Entry;
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
    35
import java.util.Iterator;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.util.HashSet;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.util.HashMap;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.ByteBuffer;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import java.nio.channels.SelectionKey;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import java.nio.channels.spi.SelectorProvider;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.AbstractNotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
import com.sun.nio.sctp.Association;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
import com.sun.nio.sctp.AssociationChangeNotification;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
import com.sun.nio.sctp.HandlerResult;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
import com.sun.nio.sctp.IllegalReceiveException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
import com.sun.nio.sctp.InvalidStreamException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
import com.sun.nio.sctp.NotificationHandler;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
import com.sun.nio.sctp.MessageInfo;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
import com.sun.nio.sctp.SctpMultiChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
import com.sun.nio.sctp.SctpSocketOption;
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    56
import sun.nio.ch.DirectBuffer;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    57
import sun.nio.ch.NativeThread;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    58
import sun.nio.ch.IOStatus;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    59
import sun.nio.ch.IOUtil;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    60
import sun.nio.ch.Net;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    61
import sun.nio.ch.PollArrayWrapper;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    62
import sun.nio.ch.SelChImpl;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    63
import sun.nio.ch.SelectionKeyImpl;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    64
import sun.nio.ch.Util;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
    65
import static com.sun.nio.sctp.SctpStandardSocketOptions.*;
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    66
import static sun.nio.ch.sctp.ResultContainer.*;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
 * An implementation of SctpMultiChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
public class SctpMultiChannelImpl extends SctpMultiChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
    /* IDs of native threads doing send and receives, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    private volatile long receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
    private volatile long senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    /* Lock held by current receiving thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    private final Object receiveLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
    /* Lock held by current sending thread */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
    private final Object sendLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
     * DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
    /* Binding: Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
    /* Keeps a map of addresses to association, and visa versa */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
    private HashMap<SocketAddress, Association> addressMap =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                         new HashMap<SocketAddress, Association>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
    private HashMap<Association, Set<SocketAddress>> associationMap =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
                         new HashMap<Association, Set<SocketAddress>>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
    /* If an association has been shutdown mark it for removal after
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
     * the user handler has been invoked */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
    private final ThreadLocal<Association> associationToRemove =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
        new ThreadLocal<Association>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
             @Override protected Association initialValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
                 return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
    };
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
    /* A notification handler cannot invoke receive */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
    private final ThreadLocal<Boolean> receiveInvoked =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
        new ThreadLocal<Boolean>() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
             @Override protected Boolean initialValue() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
                 return Boolean.FALSE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
    };
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
    public SctpMultiChannelImpl(SelectorProvider provider)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
        //TODO: update provider, remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
        this.fd = SctpNet.socket(false /*one-to-many*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
    public SctpMultiChannel bind(SocketAddress local, int backlog)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
                    ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
                    if (isBound())
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   148
                        SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
                    InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                        new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                    SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
                    if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
                        sm.checkListen(isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
                    Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
                    InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
                    port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
                    localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   163
                    SctpNet.listen(fdVal, backlog < 1 ? 50 : backlog);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
    public SctpMultiChannel bindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
        return bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
    public SctpMultiChannel unbindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
        return bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
    private SctpMultiChannel bindUnbindAddress(InetAddress address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                                               boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
        synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
            synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                    if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                        throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
                    if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
                        throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
                    if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
                        throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
                                "Cannot add or remove addresses from a channel that is bound to the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
                    if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
                        throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
                                "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
                    if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
                            if (addr.getAddress().equals(address)) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   204
                                SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
                    } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
                        /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
                         * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                        if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                            throw new IllegalUnbindException("Cannot remove address from a channel with only one address bound");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
                        boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
                                foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
                        if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                            throw new IllegalUnbindException("Cannot remove address from a channel that is not bound to that address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
                    SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                    /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
                    if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
                        localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                    else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                        for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                            if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                                localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
                                break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
    public Set<Association> associations()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
            throws ClosedChannelException, NotYetBoundException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
            return Collections.unmodifiableSet(associationMap.keySet());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
            return port == -1 ? false : true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    private void ensureOpen() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
    private void receiverCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
            receiverThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
    private void senderCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
            senderThread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
        IOUtil.configureBlocking(fd, block);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
        synchronized (stateLock) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   292
            SctpNet.preClose(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
            if (receiverThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
                NativeThread.signal(receiverThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
            if (senderThread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
                NativeThread.signal(senderThread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
            if (!isRegistered())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
    public FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
        return fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
        return fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
    private boolean translateReadyOps(int ops, int initialOps,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
                                      SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   324
        if ((ops & Net.POLLNVAL) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   331
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   337
        if (((ops & Net.POLLIN) != 0) &&
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
            ((intOps & SelectionKey.OP_READ) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
            newOps |= SelectionKey.OP_READ;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   341
        if (((ops & Net.POLLOUT) != 0) &&
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
            ((intOps & SelectionKey.OP_WRITE) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
            newOps |= SelectionKey.OP_WRITE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
        sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
        return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   354
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
        int newOps = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
        if ((ops & SelectionKey.OP_READ) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   363
            newOps |= Net.POLLIN;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
        if ((ops & SelectionKey.OP_WRITE) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   365
            newOps |= Net.POLLOUT;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
        sk.selector.putEventOps(sk, newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   368
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   370
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   371
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   372
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   373
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   374
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   375
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   376
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   377
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   378
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   379
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   380
            /* Postpone the kill if there is a thread sending or receiving. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   381
            if (receiverThread == 0 && senderThread == 0) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3320
diff changeset
   382
                SctpNet.close(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   383
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   384
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   385
                state = ChannelState.KILLPENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   386
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   387
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   388
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   389
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   390
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   391
    public <T> SctpMultiChannel setOption(SctpSocketOption<T> name,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   392
                                          T value,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   393
                                          Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   394
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   395
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   396
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   397
        if (!(supportedOptions().contains(name)))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   398
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   399
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   400
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   401
            if (association != null && (name.equals(SCTP_PRIMARY_ADDR) ||
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   402
                    name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   403
                checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   404
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   405
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   406
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   407
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   408
            int assocId = association == null ? 0 : association.associationID();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   409
            SctpNet.setSocketOption(fdVal, name, value, assocId);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   410
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   411
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   412
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   413
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   414
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   415
    @SuppressWarnings("unchecked")
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   416
    public <T> T getOption(SctpSocketOption<T> name, Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   417
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   418
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   419
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   420
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   421
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   422
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   423
        synchronized (stateLock) {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   424
            if (association != null && (name.equals(SCTP_PRIMARY_ADDR) ||
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   425
                    name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   426
                checkAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   427
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   428
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   429
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   430
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   431
            int assocId = association == null ? 0 : association.associationID();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   432
            return (T)SctpNet.getSocketOption(fdVal, name, assocId);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   433
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   434
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   435
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   436
    private static class DefaultOptionsHolder {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   437
        static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   438
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   439
        private static Set<SctpSocketOption<?>> defaultOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   440
            HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(10);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   441
            set.add(SCTP_DISABLE_FRAGMENTS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   442
            set.add(SCTP_EXPLICIT_COMPLETE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   443
            set.add(SCTP_FRAGMENT_INTERLEAVE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   444
            set.add(SCTP_INIT_MAXSTREAMS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   445
            set.add(SCTP_NODELAY);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   446
            set.add(SCTP_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   447
            set.add(SCTP_SET_PEER_PRIMARY_ADDR);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   448
            set.add(SO_SNDBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   449
            set.add(SO_RCVBUF);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   450
            set.add(SO_LINGER);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   451
            return Collections.unmodifiableSet(set);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   452
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   453
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   454
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   455
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   456
    public final Set<SctpSocketOption<?>> supportedOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   457
        return DefaultOptionsHolder.defaultOptions;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   458
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   459
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   460
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   461
    public <T> MessageInfo receive(ByteBuffer buffer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   462
                                   T attachment,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   463
                                   NotificationHandler<T> handler)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   464
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   465
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   466
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   467
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   468
        if (buffer.isReadOnly())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   469
            throw new IllegalArgumentException("Read-only buffer");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   470
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   471
        if (receiveInvoked.get())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   472
            throw new IllegalReceiveException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   473
                    "cannot invoke receive from handler");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   474
        receiveInvoked.set(Boolean.TRUE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   475
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   476
        try {
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   477
            ResultContainer resultContainer = new ResultContainer();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   478
            do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   479
                resultContainer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   480
                synchronized (receiveLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   481
                    ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   482
                    if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   483
                        throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   484
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   485
                    int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   486
                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   487
                        begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   488
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   489
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   490
                            if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   491
                                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   492
                            receiverThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   493
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   494
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   495
                        do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   496
                            n = receive(fdVal, buffer, resultContainer);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   497
                        } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   498
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   499
                    } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   500
                        receiverCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   501
                        end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   502
                        assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   503
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   504
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   505
                    if (!resultContainer.isNotification()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   506
                        /* message or nothing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   507
                        if (resultContainer.hasSomething()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   508
                            /* Set the association before returning */
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   509
                            MessageInfoImpl info =
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   510
                                    resultContainer.getMessageInfo();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   511
                            info.setAssociation(lookupAssociation(info.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   512
                                    associationID()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   513
                            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   514
                            if (sm != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   515
                                InetSocketAddress isa  = (InetSocketAddress)info.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   516
                                if (!addressMap.containsKey(isa)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   517
                                    /* must be a new association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   518
                                    try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   519
                                        sm.checkAccept(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   520
                                                       isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   521
                                    } catch (SecurityException se) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   522
                                        buffer.clear();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   523
                                        throw se;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   524
                                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   525
                                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   526
                            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   527
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   528
                            assert info.association() != null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   529
                            return info;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   530
                        } else  {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   531
                          /* Non-blocking may return null if nothing available*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   532
                            return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   533
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   534
                    } else { /* notification */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   535
                        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   536
                            handleNotificationInternal(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   537
                                    resultContainer);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   538
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   539
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   540
                } /* receiveLock */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   541
            } while (handler == null ? true :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   542
                (invokeNotificationHandler(resultContainer, handler, attachment)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   543
                 == HandlerResult.CONTINUE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   544
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   545
            receiveInvoked.set(Boolean.FALSE);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   546
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   547
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   548
        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   549
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   550
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   551
    private int receive(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   552
                        ByteBuffer dst,
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   553
                        ResultContainer resultContainer)
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   554
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   555
        int pos = dst.position();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   556
        int lim = dst.limit();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   557
        assert (pos <= lim);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   558
        int rem = (pos <= lim ? lim - pos : 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   559
        if (dst instanceof DirectBuffer && rem > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   560
            return receiveIntoNativeBuffer(fd, resultContainer, dst, rem, pos);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   561
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   562
        /* Substitute a native buffer. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   563
        int newSize = Math.max(rem, 1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   564
        ByteBuffer bb = Util.getTemporaryDirectBuffer(newSize);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   565
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   566
            int n = receiveIntoNativeBuffer(fd, resultContainer, bb, newSize, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   567
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   568
            if (n > 0 && rem > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   569
                dst.put(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   570
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   571
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   572
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   573
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   574
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   575
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   576
    private int receiveIntoNativeBuffer(int fd,
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   577
                                        ResultContainer resultContainer,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   578
                                        ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   579
                                        int rem,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   580
                                        int pos)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   581
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   582
        int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   583
        if (n > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   584
            bb.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   585
        return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   586
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   587
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   588
    private InternalNotificationHandler internalNotificationHandler =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   589
            new InternalNotificationHandler();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   590
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   591
    private void handleNotificationInternal(ResultContainer resultContainer)
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   592
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   593
        invokeNotificationHandler(resultContainer,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   594
                internalNotificationHandler, null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   595
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   596
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   597
    private class InternalNotificationHandler
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   598
            extends AbstractNotificationHandler<Object>
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   599
    {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   600
        @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   601
        public HandlerResult handleNotification(
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   602
                AssociationChangeNotification not, Object unused) {
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   603
            AssociationChange sac = (AssociationChange) not;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   604
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   605
            /* Update map to reflect change in association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   606
            switch (not.event()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   607
                case COMM_UP :
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   608
                    Association newAssociation = new AssociationImpl
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   609
                       (sac.assocId(), sac.maxInStreams(), sac.maxOutStreams());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   610
                    addAssociation(newAssociation);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   611
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   612
                case SHUTDOWN :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   613
                case COMM_LOST :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   614
                //case RESTART: ???
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   615
                    /* mark association for removal after user handler invoked*/
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   616
                    associationToRemove.set(lookupAssociation(sac.assocId()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   617
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   618
            return HandlerResult.CONTINUE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   619
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   620
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   621
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   622
    private <T> HandlerResult invokeNotificationHandler(
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   623
                                   ResultContainer resultContainer,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   624
                                   NotificationHandler<T> handler,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   625
                                   T attachment) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   626
        HandlerResult result;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   627
        SctpNotification notification = resultContainer.notification();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   628
        notification.setAssociation(lookupAssociation(notification.assocId()));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   629
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   630
        if (!(handler instanceof AbstractNotificationHandler)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   631
            result = handler.handleNotification(notification, attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   632
        } else { /* AbstractNotificationHandler */
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   633
            AbstractNotificationHandler<T> absHandler =
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   634
                    (AbstractNotificationHandler<T>)handler;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   635
            switch(resultContainer.type()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   636
                case ASSOCIATION_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   637
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   638
                            resultContainer.getAssociationChanged(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   639
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   640
                case PEER_ADDRESS_CHANGED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   641
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   642
                            resultContainer.getPeerAddressChanged(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   643
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   644
                case SEND_FAILED :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   645
                    result = absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   646
                            resultContainer.getSendFailed(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   647
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   648
                case SHUTDOWN :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   649
                    result =  absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   650
                            resultContainer.getShutdown(), attachment);
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   651
                    break;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   652
                default :
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   653
                    /* implementation specific handlers */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   654
                    result =  absHandler.handleNotification(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   655
                            resultContainer.notification(), attachment);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   656
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   657
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   658
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   659
        if (!(handler instanceof InternalNotificationHandler)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   660
            /* Only remove associations after user handler
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   661
             * has finished with them */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   662
            Association assoc = associationToRemove.get();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   663
            if (assoc != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   664
                removeAssociation(assoc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   665
                associationToRemove.set(null);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   666
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   667
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   668
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   669
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   670
        return result;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   671
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   672
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   673
    private Association lookupAssociation(int assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   674
        /* Lookup the association in our internal map */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   675
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   676
            Set<Association> assocs = associationMap.keySet();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   677
            for (Association a : assocs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   678
                if (a.associationID() == assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   679
                    return a;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   680
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   681
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   682
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   683
        return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   684
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   685
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   686
    private void addAssociation(Association association) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   687
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   688
            int assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   689
            Set<SocketAddress> addresses = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   690
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   691
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   692
                addresses = SctpNet.getRemoteAddresses(fdVal, assocId);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   693
            } catch (IOException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   694
                /* OK, determining connected addresses may not be possible
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   695
                 * shutdown, connection lost, etc */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   696
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   697
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   698
            associationMap.put(association, addresses);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   699
            if (addresses != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   700
                for (SocketAddress addr : addresses)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   701
                    addressMap.put(addr, association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   702
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   703
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   704
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   705
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   706
    private void removeAssociation(Association association) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   707
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   708
            int assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   709
            Set<SocketAddress> addresses = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   710
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   711
             try {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   712
                addresses = SctpNet.getRemoteAddresses(fdVal, assocId);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   713
            } catch (IOException unused) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   714
                /* OK, determining connected addresses may not be possible
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   715
                 * shutdown, connection lost, etc */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   716
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   717
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   718
            Set<Association> assocs = associationMap.keySet();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   719
            for (Association a : assocs) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   720
                if (a.associationID() == assocId) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   721
                    associationMap.remove(a);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   722
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   723
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   724
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   725
            if (addresses != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   726
                for (SocketAddress addr : addresses)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   727
                    addressMap.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   728
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   729
                /* We cannot determine the connected addresses */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   730
                Set<java.util.Map.Entry<SocketAddress, Association>> addrAssocs =
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   731
                        addressMap.entrySet();
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   732
                Iterator<Entry<SocketAddress, Association>> iterator = addrAssocs.iterator();
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   733
                while (iterator.hasNext()) {
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   734
                    Entry<SocketAddress, Association> entry = iterator.next();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   735
                    if (entry.getValue().equals(association)) {
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   736
                        iterator.remove();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   737
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   738
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   739
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   740
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   741
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   742
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   743
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   744
     * @throws  IllegalArgumentException
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   745
     *          If the given association is not controlled by this channel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   746
     *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   747
     * @return  {@code true} if, and only if, the given association is one
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   748
     *          of the current associations controlled by this channel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   749
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   750
    private boolean checkAssociation(Association messageAssoc) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   751
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   752
            for (Association association : associationMap.keySet()) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   753
                if (messageAssoc.equals(association)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   754
                    return true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   755
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   756
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   757
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   758
        throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   759
              "Given Association is not controlled by this channel");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   760
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   761
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   762
    private void checkStreamNumber(Association assoc, int streamNumber) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   763
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   764
            if (streamNumber < 0 || streamNumber >= assoc.maxOutboundStreams())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   765
                throw new InvalidStreamException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   766
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   767
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   768
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   769
    /* TODO: Add support for ttl and isComplete to both 121 12M
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   770
     *       SCTP_EOR not yet supported on reference platforms
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   771
     *       TTL support limited...
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   772
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   773
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   774
    public int send(ByteBuffer buffer, MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   775
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   776
        if (buffer == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   777
            throw new IllegalArgumentException("buffer cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   778
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   779
        if (messageInfo == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   780
            throw new IllegalArgumentException("messageInfo cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   781
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   782
        synchronized (sendLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   783
            ensureOpen();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   784
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   785
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   786
                bind(null, 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   787
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   788
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   789
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   790
                int assocId = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   791
                SocketAddress address = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   792
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   793
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   794
                synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   795
                    if(!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   796
                        return 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   797
                    senderThread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   798
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   799
                    /* Determine what address or association to send to */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   800
                    Association assoc = messageInfo.association();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   801
                    InetSocketAddress addr = (InetSocketAddress)messageInfo.address();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   802
                    if (assoc != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   803
                        checkAssociation(assoc);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   804
                        checkStreamNumber(assoc, messageInfo.streamNumber());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   805
                        assocId = assoc.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   806
                        /* have we also got a preferred address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   807
                        if (addr != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   808
                            if (!assoc.equals(addressMap.get(addr)))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   809
                                throw new IllegalArgumentException("given preferred address is not part of this association");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   810
                            address = addr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   811
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   812
                    } else if (addr != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   813
                        address = addr;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   814
                        Association association = addressMap.get(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   815
                        if (association != null) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   816
                            checkStreamNumber(association, messageInfo.streamNumber());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   817
                            assocId = association.associationID();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   818
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   819
                        } else { /* must be new association */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   820
                            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   821
                            if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   822
                                sm.checkConnect(addr.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   823
                                                addr.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   824
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   825
                    } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   826
                        throw new AssertionError(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   827
                            "Both association and address cannot be null");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   828
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   829
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   830
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   831
                do {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   832
                    n = send(fdVal, buffer, assocId, address, messageInfo);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   833
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   834
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   835
                return IOStatus.normalize(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   836
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   837
                senderCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   838
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   839
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   840
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   841
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   842
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   843
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   844
    private int send(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   845
                     ByteBuffer src,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   846
                     int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   847
                     SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   848
                     MessageInfo messageInfo)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   849
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   850
        int streamNumber = messageInfo.streamNumber();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   851
        boolean unordered = messageInfo.isUnordered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   852
        int ppid = messageInfo.payloadProtocolID();
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   853
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   854
        if (src instanceof DirectBuffer)
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   855
            return sendFromNativeBuffer(fd, src, target, assocId,
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   856
                    streamNumber, unordered, ppid);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   857
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   858
        /* Substitute a native buffer */
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   859
        int pos = src.position();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   860
        int lim = src.limit();
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   861
        assert (pos <= lim && streamNumber >= 0);
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   862
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   863
        int rem = (pos <= lim ? lim - pos : 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   864
        ByteBuffer bb = Util.getTemporaryDirectBuffer(rem);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   865
        try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   866
            bb.put(src);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   867
            bb.flip();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   868
            /* Do not update src until we see how many bytes were written */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   869
            src.position(pos);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   870
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   871
            int n = sendFromNativeBuffer(fd, bb, target, assocId,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   872
                    streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   873
            if (n > 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   874
                /* now update src */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   875
                src.position(pos + n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   876
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   877
            return n;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   878
        } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   879
            Util.releaseTemporaryDirectBuffer(bb);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   880
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   881
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   882
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   883
    private int sendFromNativeBuffer(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   884
                                     ByteBuffer bb,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   885
                                     SocketAddress target,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   886
                                     int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   887
                                     int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   888
                                     boolean unordered,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   889
                                     int ppid)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   890
            throws IOException {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   891
        InetAddress addr = null;     // no preferred address
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   892
        int port = 0;
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   893
        if (target != null) {
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   894
            InetSocketAddress isa = Net.checkAddress(target);
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   895
            addr = isa.getAddress();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   896
            port = isa.getPort();
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   897
        }
4677
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   898
        int pos = bb.position();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   899
        int lim = bb.limit();
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   900
        assert (pos <= lim);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   901
        int rem = (pos <= lim ? lim - pos : 0);
1b6ce3fbc01b 6916890: (sctp) SctpChannel.send may cause IAE if given a heap buffer with an offset
chegar
parents: 4669
diff changeset
   902
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   903
        int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   904
                            port, assocId, streamNumber, unordered, ppid);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   905
        if (written > 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   906
            bb.position(pos + written);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   907
        return written;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   908
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   909
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   910
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   911
    public SctpMultiChannel shutdown(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   912
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   913
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   914
            checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   915
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   916
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   917
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   918
            SctpNet.shutdown(fdVal, association.associationID());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   919
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   920
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   921
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   922
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   923
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   924
    public Set<SocketAddress> getAllLocalAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   925
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   926
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   927
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   928
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   929
            if (!isBound())
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   930
                return Collections.emptySet();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   931
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   932
            return SctpNet.getLocalAddresses(fdVal);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   933
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   934
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   935
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   936
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   937
    public Set<SocketAddress> getRemoteAddresses(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   938
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   939
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   940
            checkAssociation(association);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   941
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   942
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   943
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   944
            try {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   945
                return SctpNet.getRemoteAddresses(fdVal, association.associationID());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   946
            } catch (SocketException se) {
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   947
                /* a valid association should always have remote addresses */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   948
                Set<SocketAddress> addrs = associationMap.get(association);
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   949
                return addrs != null ? addrs : Collections.<SocketAddress>emptySet();
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   950
            }
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   951
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   952
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   953
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   954
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   955
    public SctpChannel branch(Association association)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   956
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   957
        synchronized (stateLock) {
3072
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   958
            checkAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   959
            if (!isOpen())
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   960
                throw new ClosedChannelException();
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   961
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   962
            FileDescriptor bFd = SctpNet.branch(fdVal,
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   963
                                                association.associationID());
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   964
            /* successfully branched, we can now remove it from assoc list */
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   965
            removeAssociation(association);
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   966
a801b122142f 6855335: Several changes in the SCTP implementation.
chegar
parents: 2542
diff changeset
   967
            return new SctpChannelImpl(provider(), bFd, association);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   968
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   969
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   970
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   971
    /* Use common native implementation shared between
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   972
     * one-to-one and one-to-many */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   973
    private static int receive0(int fd,
11823
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
   974
                                ResultContainer resultContainer,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   975
                                long address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   976
                                int length)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   977
            throws IOException{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   978
        return SctpChannelImpl.receive0(fd, resultContainer, address,
3320
a7c037dd2e14 6863110: Newly connected/accepted SctpChannel should fire OP_READ if registered with a Selector
chegar
parents: 3072
diff changeset
   979
                length, false /*peek */);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   980
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   981
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   982
    private static int send0(int fd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   983
                             long address,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   984
                             int length,
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   985
                             InetAddress addr,
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   986
                             int port,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   987
                             int assocId,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   988
                             int streamNumber,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   989
                             boolean unordered,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   990
                             int ppid)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   991
            throws IOException {
16089
7cf1e2708454 7201071: InetSocketAddress serialization issue
chegar
parents: 12559
diff changeset
   992
        return SctpChannelImpl.send0(fd, address, length, addr, port, assocId,
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   993
                streamNumber, unordered, ppid);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   994
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   995
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   996
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 16100
diff changeset
   997
        IOUtil.load();   /* loads nio & net native libraries */
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   998
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   999
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
  1000
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
  1001
                    System.loadLibrary("sctp");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
  1002
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
  1003
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
  1004
            });
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1005
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
  1006
}