src/jdk.sctp/unix/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java
author jcbeyler
Wed, 14 Nov 2018 12:25:15 -0800
changeset 52561 40098289d580
parent 49493 814bd31f8da0
child 54154 1caf2daef7cf
permissions -rw-r--r--
8213622: Windows VS2013 build failure - "'snprintf': identifier not found" Summary: Replace snprintf with strlen and memcpy Reviewed-by: dholmes, mvala, kbarrett
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: 4669
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: 4669
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: 4669
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4669
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4669
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.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.ClosedChannelException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    37
import java.nio.channels.NotYetBoundException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    38
import java.nio.channels.spi.SelectorProvider;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    39
import com.sun.nio.sctp.IllegalUnbindException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    40
import com.sun.nio.sctp.SctpChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    41
import com.sun.nio.sctp.SctpServerChannel;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    42
import com.sun.nio.sctp.SctpSocketOption;
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
    43
import 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
    44
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
    45
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
    46
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
    47
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
    48
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
    49
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
    50
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
    51
import sun.nio.ch.Util;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    52
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
 * An implementation of SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
public class SctpServerChannelImpl extends SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
    /* IDs of native thread doing accept, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
    private volatile long thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
    /* Lock held by thread currently blocked in this channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
    private final Object lock = 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
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
     * DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
        INUSE,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
    /* Binding: Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
     * Initializes a new instance of this class.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
    public SctpServerChannelImpl(SelectorProvider provider)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
        //TODO: update provider remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
        this.fd = SctpNet.socket(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
        this.state = ChannelState.INUSE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
    public SctpServerChannel bind(SocketAddress local, int backlog)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                if (isBound())
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   110
                    SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   111
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
                InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                    new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
                SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
                if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
                    sm.checkListen(isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
                Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
                InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
                port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
                localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   125
                SctpNet.listen(fdVal, backlog < 1 ? 50 : backlog);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   126
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
        return this;
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
    public SctpServerChannel bindAddress(InetAddress address)
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
        return bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
    public SctpServerChannel unbindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   140
        return bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   141
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
    private SctpServerChannel bindUnbindAddress(InetAddress address, boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
                    throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
                if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
                    throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                            "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
   157
                if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   158
                    throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
                            "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
                if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
                        if (addr.getAddress().equals(address)) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   163
                            SctpNet.throwAlreadyBoundException();
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
                } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
                    /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
                     * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
                    if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
                        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
   171
                    boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   172
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
                            foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
                            break;
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
                    if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
                        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
   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
                SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
                /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                    localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                            localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                            break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   193
                    }
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
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   197
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   199
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   200
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   201
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
            return port == -1 ? false : true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   204
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   205
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   206
    private void acceptCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
            thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
                kill();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   212
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   213
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   214
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
    public SctpChannel accept() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
                throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
            FileDescriptor newfd = new FileDescriptor();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
            InetSocketAddress[] isaa = new InetSocketAddress[1];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                    return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                thread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
                for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
                    n = accept0(fd, newfd, isaa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
                        continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
                acceptCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                end(n > 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
            if (n < 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
            IOUtil.configureBlocking(newfd, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
            InetSocketAddress isa = isaa[0];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
            sc = new SctpChannelImpl(provider(), newfd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
            if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
                sm.checkAccept(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
                               isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
            return sc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
        }
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
        IOUtil.configureBlocking(fd, block);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
        synchronized (stateLock) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   268
            SctpNet.preClose(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   269
            if (thread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
                NativeThread.signal(thread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
            if (!isRegistered())
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
            // Postpone the kill if there is a thread in accept
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
            if (thread == 0) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   289
                SctpNet.close(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   290
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
                state = ChannelState.KILLPENDING;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   294
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   295
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   296
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   297
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   298
    public FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   299
        return fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   301
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   302
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   303
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
        return fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   306
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   307
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   308
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
    private boolean translateReadyOps(int ops, int initialOps,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
                                     SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   316
        if ((ops & Net.POLLNVAL) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   317
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   323
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   324
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   329
        if (((ops & Net.POLLIN) != 0) &&
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   330
            ((intOps & SelectionKey.OP_ACCEPT) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
                newOps |= SelectionKey.OP_ACCEPT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
    @Override
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49417
diff changeset
   348
    public int translateInterestOps(int ops) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
        int newOps = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
        if ((ops & SelectionKey.OP_ACCEPT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   351
            newOps |= Net.POLLIN;
49493
814bd31f8da0 8200257: (se) More Selector cleanup
alanb
parents: 49417
diff changeset
   352
        return newOps;
2542
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
    public <T> SctpServerChannel setOption(SctpSocketOption<T> name, T value)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   363
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   365
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
            SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   368
            return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
        }
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
    @Override
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   373
    @SuppressWarnings("unchecked")
2542
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);
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   393
            set.add(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
2542
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())
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   410
                return Collections.emptySet();
2542
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 {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   423
        IOUtil.load();   // loads nio & net native libraries
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   424
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   425
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   426
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   427
                    System.loadLibrary("sctp");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   428
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   429
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   430
            });
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   431
        initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   432
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   433
}