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