jdk/src/solaris/classes/sun/nio/ch/sctp/SctpServerChannelImpl.java
author simonis
Mon, 20 Jan 2014 09:24:25 +0100
changeset 22604 9b394795e216
parent 19607 bee007586d06
child 23010 6dadb192ad81
permissions -rw-r--r--
8031997: PPC64: Make the various POLL constants system dependant Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12559
diff changeset
     2
 * Copyright (c) 2009, 2012, 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.PollArrayWrapper;
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.SelChImpl;
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.SelectionKeyImpl;
ee83ae88512d 7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents: 10137
diff changeset
    52
import sun.nio.ch.Util;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    53
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    54
/**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    55
 * An implementation of SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    56
 */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    57
public class SctpServerChannelImpl extends SctpServerChannel
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    58
    implements SelChImpl
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    59
{
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    60
    private final FileDescriptor fd;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    61
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    62
    private final int fdVal;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    63
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    64
    /* IDs of native thread doing accept, for signalling */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    65
    private volatile long thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    66
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    67
    /* Lock held by thread currently blocked in this channel */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    68
    private final Object lock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    69
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    70
    /* Lock held by any thread that modifies the state fields declared below
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    71
     * DO NOT invoke a blocking I/O operation while holding this lock! */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    72
    private final Object stateLock = new Object();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    73
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    74
    private enum ChannelState {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    75
        UNINITIALIZED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    76
        INUSE,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    77
        KILLPENDING,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    78
        KILLED,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    79
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    80
    /* -- The following fields are protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    81
    private ChannelState state = ChannelState.UNINITIALIZED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    82
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    83
    /* Binding: Once bound the port will remain constant. */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    84
    int port = -1;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    85
    private HashSet<InetSocketAddress> localAddresses = new HashSet<InetSocketAddress>();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    86
    /* Has the channel been bound to the wildcard address */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    87
    private boolean wildcard; /* false */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    88
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    89
    /* -- End of fields protected by stateLock -- */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    90
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    91
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    92
     * Initializes a new instance of this class.
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    93
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    94
    public SctpServerChannelImpl(SelectorProvider provider)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    95
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    96
        //TODO: update provider remove public modifier
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    97
        super(provider);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    98
        this.fd = SctpNet.socket(true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
    99
        this.fdVal = IOUtil.fdVal(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   100
        this.state = ChannelState.INUSE;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   101
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   102
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   103
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   104
    public SctpServerChannel bind(SocketAddress local, int backlog)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   105
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   106
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   107
            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   108
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   109
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   110
                if (isBound())
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   111
                    SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   112
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   113
                InetSocketAddress isa = (local == null) ?
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   114
                    new InetSocketAddress(0) : Net.checkAddress(local);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   115
                SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   116
                if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   117
                    sm.checkListen(isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   118
                Net.bind(fd, isa.getAddress(), isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   119
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   120
                InetSocketAddress boundIsa = Net.localAddress(fd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   121
                port = boundIsa.getPort();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   122
                localAddresses.add(isa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   123
                    if (isa.getAddress().isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   124
                        wildcard = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   125
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   126
                SctpNet.listen(fdVal, backlog < 1 ? 50 : backlog);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   127
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   128
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   129
        return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   130
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   131
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   132
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   133
    public SctpServerChannel bindAddress(InetAddress address)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   134
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   135
        return bindUnbindAddress(address, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   136
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   137
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   138
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   139
    public SctpServerChannel unbindAddress(InetAddress address)
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
        return bindUnbindAddress(address, false);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   142
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   143
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   144
    private SctpServerChannel bindUnbindAddress(InetAddress address, boolean add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   145
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   146
        if (address == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   147
            throw new IllegalArgumentException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   148
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   149
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   150
            synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   151
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   152
                    throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   153
                if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   154
                    throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   155
                if (wildcard)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   156
                    throw new IllegalStateException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   157
                            "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
   158
                if (address.isAnyLocalAddress())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   159
                    throw new IllegalArgumentException(
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   160
                            "Cannot add or remove the wildcard address");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   161
                if (add) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   162
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   163
                        if (addr.getAddress().equals(address)) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   164
                            SctpNet.throwAlreadyBoundException();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   165
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   166
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   167
                } else { /*removing */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   168
                    /* Verify that there is more than one address
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   169
                     * and that address is already bound */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   170
                    if (localAddresses.size() <= 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   171
                        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
   172
                    boolean foundAddress = false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   173
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   174
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   175
                            foundAddress = true;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   176
                            break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   177
                        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   178
                    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   179
                    if (!foundAddress )
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   180
                        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
   181
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   182
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   183
                SctpNet.bindx(fdVal, new InetAddress[]{address}, port, add);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   184
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   185
                /* Update our internal Set to reflect the addition/removal */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   186
                if (add)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   187
                    localAddresses.add(new InetSocketAddress(address, port));
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   188
                else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   189
                    for (InetSocketAddress addr : localAddresses) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   190
                        if (addr.getAddress().equals(address)) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   191
                            localAddresses.remove(addr);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   192
                            break;
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
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   198
        return this;
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
    private boolean isBound() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   202
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   203
            return port == -1 ? false : true;
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   207
    private void acceptCleanup() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   208
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   209
            thread = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   210
            if (state == ChannelState.KILLPENDING)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   211
                kill();
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   215
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   216
    public SctpChannel accept() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   217
        synchronized (lock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   218
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   219
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   220
            if (!isBound())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   221
                throw new NotYetBoundException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   222
            SctpChannel sc = null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   223
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   224
            int n = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   225
            FileDescriptor newfd = new FileDescriptor();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   226
            InetSocketAddress[] isaa = new InetSocketAddress[1];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   227
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   228
            try {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   229
                begin();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   230
                if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   231
                    return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   232
                thread = NativeThread.current();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   233
                for (;;) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   234
                    n = accept0(fd, newfd, isaa);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   235
                    if ((n == IOStatus.INTERRUPTED) && isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   236
                        continue;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   237
                    break;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   238
                }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   239
            } finally {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   240
                acceptCleanup();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   241
                end(n > 0);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   242
                assert IOStatus.check(n);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   243
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   244
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   245
            if (n < 1)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   246
                return null;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   247
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   248
            IOUtil.configureBlocking(newfd, true);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   249
            InetSocketAddress isa = isaa[0];
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   250
            sc = new SctpChannelImpl(provider(), newfd);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   251
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   252
            SecurityManager sm = System.getSecurityManager();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   253
            if (sm != null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   254
                sm.checkAccept(isa.getAddress().getHostAddress(),
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   255
                               isa.getPort());
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   256
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   257
            return sc;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   258
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   259
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   260
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   261
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   262
    protected void implConfigureBlocking(boolean block) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   263
        IOUtil.configureBlocking(fd, block);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   264
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   265
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   266
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   267
    public void implCloseSelectableChannel() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   268
        synchronized (stateLock) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   269
            SctpNet.preClose(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   270
            if (thread != 0)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   271
                NativeThread.signal(thread);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   272
            if (!isRegistered())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   273
                kill();
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
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   277
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   278
    public void kill() throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   279
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   280
            if (state == ChannelState.KILLED)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   281
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   282
            if (state == ChannelState.UNINITIALIZED) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   283
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   284
                return;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   285
            }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   286
            assert !isOpen() && !isRegistered();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   287
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   288
            // Postpone the kill if there is a thread in accept
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   289
            if (thread == 0) {
4669
11d1dbd3598d 6915313: Reorganize implementation to make it more feasible to port to JDK6
chegar
parents: 3072
diff changeset
   290
                SctpNet.close(fdVal);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   291
                state = ChannelState.KILLED;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   292
            } else {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   293
                state = ChannelState.KILLPENDING;
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
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 FileDescriptor getFD() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   300
        return fd;
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
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   304
    public int getFDVal() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   305
        return fdVal;
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
    /**
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   309
     * Translates native poll revent ops into a ready operation ops
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   310
     */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   311
    private boolean translateReadyOps(int ops, int initialOps,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   312
                                     SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   313
        int intOps = sk.nioInterestOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   314
        int oldOps = sk.nioReadyOps();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   315
        int newOps = initialOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   316
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   317
        if ((ops & Net.POLLNVAL) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   318
            /* This should only happen if this channel is pre-closed while a
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   319
             * selection operation is in progress
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   320
             * ## Throw an error if this channel has not been pre-closed */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   321
            return false;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   322
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   323
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   324
        if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) {
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   325
            newOps = intOps;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   326
            sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   327
            return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   328
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   329
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   330
        if (((ops & Net.POLLIN) != 0) &&
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   331
            ((intOps & SelectionKey.OP_ACCEPT) != 0))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   332
                newOps |= SelectionKey.OP_ACCEPT;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   333
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   334
        sk.nioReadyOps(newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   335
        return (newOps & ~oldOps) != 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   336
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   337
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   338
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   339
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   340
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   341
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   342
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   343
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   344
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   345
        return translateReadyOps(ops, 0, sk);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   346
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   347
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   348
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   349
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   350
        int newOps = 0;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   351
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   352
        /* Translate ops */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   353
        if ((ops & SelectionKey.OP_ACCEPT) != 0)
22604
9b394795e216 8031997: PPC64: Make the various POLL constants system dependant
simonis
parents: 19607
diff changeset
   354
            newOps |= Net.POLLIN;
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   355
        /* Place ops into pollfd array */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   356
        sk.selector.putEventOps(sk, newOps);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   357
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   358
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   359
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   360
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   361
    public <T> SctpServerChannel setOption(SctpSocketOption<T> name, T value)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   362
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   363
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   364
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   365
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   366
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   367
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   368
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   369
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   370
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   371
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   372
            SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   373
            return this;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   374
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   375
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   376
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   377
    @Override
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   378
    @SuppressWarnings("unchecked")
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   379
    public <T> T getOption(SctpSocketOption<T> name) throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   380
        if (name == null)
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   381
            throw new NullPointerException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   382
        if (!supportedOptions().contains(name))
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   383
            throw new UnsupportedOperationException("'" + name + "' not supported");
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   384
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   385
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   386
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   387
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   388
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   389
            return (T) SctpNet.getSocketOption(fdVal, name, 0 /*oneToOne*/);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   390
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   391
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   392
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   393
    private static class DefaultOptionsHolder {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   394
        static final Set<SctpSocketOption<?>> defaultOptions = defaultOptions();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   395
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   396
        private static Set<SctpSocketOption<?>> defaultOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   397
            HashSet<SctpSocketOption<?>> set = new HashSet<SctpSocketOption<?>>(1);
9679
d98ae8bc45fc 7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents: 7668
diff changeset
   398
            set.add(SctpStandardSocketOptions.SCTP_INIT_MAXSTREAMS);
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   399
            return Collections.unmodifiableSet(set);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   400
        }
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 final Set<SctpSocketOption<?>> supportedOptions() {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   405
        return DefaultOptionsHolder.defaultOptions;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   406
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   407
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   408
    @Override
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   409
    public Set<SocketAddress> getAllLocalAddresses()
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   410
            throws IOException {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   411
        synchronized (stateLock) {
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   412
            if (!isOpen())
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   413
                throw new ClosedChannelException();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   414
            if (!isBound())
10137
d92637d3d673 7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9679
diff changeset
   415
                return Collections.emptySet();
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   416
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   417
            return SctpNet.getLocalAddresses(fdVal);
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   418
        }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   419
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   420
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   421
    /* Native */
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   422
    private static native void initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   423
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   424
    private static native int accept0(FileDescriptor ssfd,
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   425
        FileDescriptor newfd, InetSocketAddress[] isaa) throws IOException;
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   426
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   427
    static {
19607
bee007586d06 8022594: Potential deadlock in <clinit> of sun.nio.ch.Util/IOUtil
alanb
parents: 14342
diff changeset
   428
        IOUtil.load();   // loads nio & net native libraries
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   429
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   430
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   431
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   432
                    System.loadLibrary("sctp");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   433
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   434
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 11823
diff changeset
   435
            });
2542
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   436
        initIDs();
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   437
    }
d859108aea12 4927640: Implementation of the sctp protocol
chegar
parents:
diff changeset
   438
}