jdk/src/solaris/classes/sun/nio/ch/SctpServerChannelImpl.java
author chegar
Thu, 16 Apr 2009 17:42:00 +0100
changeset 2542 d859108aea12
child 3072 a801b122142f
permissions -rw-r--r--
4927640: Implementation of the sctp protocol Summary: An implementation-specific API for the Stream Control Transmission Protocol Reviewed-by: alanb, michaelm, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    23
 * have any questions.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    24
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    25
package sun.nio.ch;
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.SocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    28
import java.net.InetSocketAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    29
import java.net.InetAddress;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    30
import java.io.FileDescriptor;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    31
import java.io.IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    32
import java.util.Collections;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    33
import java.util.Set;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    34
import java.util.HashSet;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    35
import java.nio.channels.SelectionKey;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    36
import java.nio.channels.AlreadyBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import java.nio.channels.spi.SelectorProvider;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    43
import com.sun.nio.sctp.SctpSocketOption;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    44
import com.sun.nio.sctp.SctpStandardSocketOption;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    45
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    46
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    47
 * An implementation of SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    48
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    49
public class SctpServerChannelImpl extends SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    50
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    51
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
    /* Used to make native close and preClose calls */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
    private static NativeDispatcher nd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
    /* IDs of native thread doing accept, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
    private volatile long thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
    /* Lock held by thread currently blocked in this channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
    private final Object lock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
     * DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
        INUSE,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
    /* Binding: Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
     * Initializes a new instance of this class.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
    public SctpServerChannelImpl(SelectorProvider provider)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
        //TODO: update provider remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
        this.fd = SctpNet.socket(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
        this.state = ChannelState.INUSE;
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
    public SctpServerChannel bind(SocketAddress local, int backlog)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
                if (isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
                    throw new AlreadyBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                    new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
                SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
                if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                    sm.checkListen(isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
                InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
                port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
                localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
                Net.listen(fd, backlog < 1 ? 50 : backlog);
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
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
    public SctpServerChannel bindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
        return bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
    public SctpServerChannel unbindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
        return bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
    private SctpServerChannel bindUnbindAddress(InetAddress address, boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
        synchronized (lock) {
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
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
                if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
                    throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                    throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                            "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
   153
                if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
                    throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
                            "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
                            throw new AlreadyBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
                } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
                    /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   164
                     * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
                    if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
                        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
   167
                    boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
                            foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
                            break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
                    if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
                        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
   176
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
                SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
                /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   181
                if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
                    localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                            localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                            break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   194
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   195
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   196
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
            return port == -1 ? false : true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
    private void acceptCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
            thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
    public SctpChannel accept() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
                throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
            FileDescriptor newfd = new FileDescriptor();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
            InetSocketAddress[] isaa = new InetSocketAddress[1];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
                    return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
                thread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                    n = accept0(fd, newfd, isaa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                        continue;
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
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
                acceptCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                end(n > 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
            if (n < 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
            IOUtil.configureBlocking(newfd, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
            InetSocketAddress isa = isaa[0];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
            sc = new SctpChannelImpl(provider(), newfd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
            if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
                sm.checkAccept(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
                               isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
            return sc;
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
        IOUtil.configureBlocking(fd, block);
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
            nd.preClose(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
            if (thread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
                NativeThread.signal(thread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
            if (!isRegistered())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   274
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   275
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   276
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
            // Postpone the kill if there is a thread in accept
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
            if (thread == 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
                nd.close(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
                state = ChannelState.KILLPENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
    public FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
        return fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
        return fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
    }
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
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
    private boolean translateReadyOps(int ops, int initialOps,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
                                     SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
        if ((ops & PollArrayWrapper.POLLNVAL) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
        if ((ops & (PollArrayWrapper.POLLERR
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
                    | PollArrayWrapper.POLLHUP)) != 0) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
        if (((ops & PollArrayWrapper.POLLIN) != 0) &&
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
            ((intOps & SelectionKey.OP_ACCEPT) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
                newOps |= SelectionKey.OP_ACCEPT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
        sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
        return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
        int newOps = 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
        /* Translate ops */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
        if ((ops & SelectionKey.OP_ACCEPT) != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
            newOps |= PollArrayWrapper.POLLIN;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
        /* Place ops into pollfd array */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
        sk.selector.putEventOps(sk, newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   354
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
    public <T> SctpServerChannel setOption(SctpSocketOption<T> name, T value)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   363
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   365
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   368
            SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
            return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   370
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   371
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   372
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   373
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   374
    public <T> T getOption(SctpSocketOption<T> name) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   375
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   376
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   377
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   378
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   379
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   380
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   381
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   382
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   383
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   384
            return (T) SctpNet.getSocketOption(fdVal, name, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   385
        }
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
    private static class DefaultOptionsHolder {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   389
        static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   390
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   391
        private static Set<SctpSocketOption<?>> defaultOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   392
            HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(1);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   393
            set.add(SctpStandardSocketOption.SCTP_INIT_MAXSTREAMS);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   394
            return Collections.unmodifiableSet(set);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   395
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   396
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   397
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   398
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   399
    public final Set<SctpSocketOption<?>> supportedOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   400
        return DefaultOptionsHolder.defaultOptions;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   401
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   402
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   403
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   404
    public Set<SocketAddress> getAllLocalAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   405
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   406
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   407
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   408
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   409
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   410
                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   411
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   412
            return SctpNet.getLocalAddresses(fdVal);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   413
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   414
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   415
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   416
    /* Native */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   417
    private static native void initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   418
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   419
    private static native int accept0(FileDescriptor ssfd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   420
        FileDescriptor newfd, InetSocketAddress[] isaa) throws IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   421
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   422
    static {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   423
        Util.load();   // loads nio & net native libraries
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   424
        java.security.AccessController.doPrivileged(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   425
                new sun.security.action.LoadLibraryAction("sctp"));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   426
        nd = new SctpSocketDispatcher();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   427
        initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   428
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   429
}