SctpNet.java 11.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24
 */
25
package sun.nio.ch.sctp;
26 27 28 29 30 31

import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
32
import java.nio.channels.AlreadyBoundException;
33 34 35 36
import java.util.Set;
import java.util.HashSet;
import java.security.AccessController;
import sun.security.action.GetPropertyAction;
37 38
import sun.nio.ch.IOUtil;
import sun.nio.ch.Net;
39
import com.sun.nio.sctp.SctpSocketOption;
40
import static com.sun.nio.sctp.SctpStandardSocketOptions.*;
41 42 43 44 45 46 47

public class SctpNet {
    static final String osName = AccessController.doPrivileged(
                    new GetPropertyAction("os.name"));

    /* -- Miscellaneous SCTP utilities -- */

48
    private static boolean IPv4MappedAddresses() {
49 50 51 52 53 54 55 56 57
        if ("SunOS".equals(osName)) {
            /* Solaris supports IPv4Mapped Addresses with bindx */
            return true;
        } /* else {  //other OS/implementations  */

        /* lksctp/linux requires Ipv4 addresses */
        return false;
    }

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    static boolean throwAlreadyBoundException() throws IOException {
        throw new AlreadyBoundException();
    }

    static void listen(int fd, int backlog) throws IOException {
        listen0(fd, backlog);
    }

    static int connect(int fd, InetAddress remote, int remotePort)
            throws IOException {
        return connect0(fd, remote, remotePort);
    }

    static void close(int fd) throws IOException {
        close0(fd);
    }

    static void preClose(int fd) throws IOException {
        preClose0(fd);
    }

79
    /**
80
     * @param  oneToOne
81 82 83 84 85 86 87 88 89 90 91
     *         if {@code true} returns a one-to-one sctp socket, otherwise
     *         returns a one-to-many sctp socket
     */
    static FileDescriptor socket(boolean oneToOne) throws IOException {
        int nativefd = socket0(oneToOne);
        return IOUtil.newFD(nativefd);
    }

    static void bindx(int fd, InetAddress[] addrs, int port, boolean add)
            throws IOException {
        bindx(fd, addrs, port, addrs.length, add,
92
                IPv4MappedAddresses());
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    }

    static Set<SocketAddress> getLocalAddresses(int fd)
            throws IOException {
        HashSet<SocketAddress> set = null;
        SocketAddress[] saa = getLocalAddresses0(fd);

        if (saa != null) {
            set = new HashSet<SocketAddress>(saa.length);
            for (SocketAddress sa : saa)
                set.add(sa);
        }

        return set;
    }

    static Set<SocketAddress> getRemoteAddresses(int fd, int assocId)
            throws IOException {
        HashSet<SocketAddress> set = null;
        SocketAddress[] saa = getRemoteAddresses0(fd, assocId);

        if (saa != null) {
            set = new HashSet<SocketAddress>(saa.length);
            for (SocketAddress sa : saa)
                set.add(sa);
        }

        return set;
    }

123 124 125 126
    static <T> void setSocketOption(int fd,
                                    SctpSocketOption<T> name,
                                    T value,
                                    int assocId)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            throws IOException {
        if (value == null)
            throw new IllegalArgumentException("Invalid option value");

        if (name.equals(SCTP_INIT_MAXSTREAMS)) {
            InitMaxStreams maxStreamValue = (InitMaxStreams)value;
            SctpNet.setInitMsgOption0(fd,
                 maxStreamValue.maxInStreams(), maxStreamValue.maxOutStreams());
        } else if (name.equals(SCTP_PRIMARY_ADDR) ||
                   name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) {

            SocketAddress addr  = (SocketAddress) value;
            if (addr == null)
                throw new IllegalArgumentException("Invalid option value");

            Net.checkAddress(addr);
            InetSocketAddress netAddr = (InetSocketAddress)addr;

            if (name.equals(SCTP_PRIMARY_ADDR)) {
146 147 148 149
                setPrimAddrOption0(fd,
                                   assocId,
                                   netAddr.getAddress(),
                                   netAddr.getPort());
150
            } else {
151 152 153 154 155
                setPeerPrimAddrOption0(fd,
                                       assocId,
                                       netAddr.getAddress(),
                                       netAddr.getPort(),
                                       IPv4MappedAddresses());
156 157 158 159 160 161 162 163 164 165 166 167 168 169
            }
        } else if (name.equals(SCTP_DISABLE_FRAGMENTS) ||
            name.equals(SCTP_EXPLICIT_COMPLETE) ||
            name.equals(SCTP_FRAGMENT_INTERLEAVE) ||
            name.equals(SCTP_NODELAY) ||
            name.equals(SO_SNDBUF) ||
            name.equals(SO_RCVBUF) ||
            name.equals(SO_LINGER)) {
            setIntOption(fd, name, value);
        } else {
            throw new AssertionError("Unknown socket option");
        }
    }

170
    static Object getSocketOption(int fd, SctpSocketOption<?> name, int assocId)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
             throws IOException {
         if (name.equals(SCTP_SET_PEER_PRIMARY_ADDR)) {
            throw new IllegalArgumentException(
                    "SCTP_SET_PEER_PRIMARY_ADDR cannot be retrieved");
        } else if (name.equals(SCTP_INIT_MAXSTREAMS)) {
            /* container for holding maxIn/Out streams */
            int[] values = new int[2];
            SctpNet.getInitMsgOption0(fd, values);
            return InitMaxStreams.create(values[0], values[1]);
        } else if (name.equals(SCTP_PRIMARY_ADDR)) {
            return getPrimAddrOption0(fd, assocId);
        } else if (name.equals(SCTP_DISABLE_FRAGMENTS) ||
            name.equals(SCTP_EXPLICIT_COMPLETE) ||
            name.equals(SCTP_FRAGMENT_INTERLEAVE) ||
            name.equals(SCTP_NODELAY) ||
            name.equals(SO_SNDBUF) ||
            name.equals(SO_RCVBUF) ||
            name.equals(SO_LINGER)) {
            return getIntOption(fd, name);
        } else {
            throw new AssertionError("Unknown socket option");
        }
    }

195
    static void setIntOption(int fd, SctpSocketOption<?> name, Object value)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            throws IOException {
        if (value == null)
            throw new IllegalArgumentException("Invalid option value");

        Class<?> type = name.type();
        if (type != Integer.class && type != Boolean.class)
            throw new AssertionError("Should not reach here");

        if (name == SO_RCVBUF ||
            name == SO_SNDBUF)
        {
            int i = ((Integer)value).intValue();
            if (i < 0)
                throw new IllegalArgumentException(
                        "Invalid send/receive buffer size");
        } else if (name == SO_LINGER) {
            int i = ((Integer)value).intValue();
            if (i < 0)
                value = Integer.valueOf(-1);
            if (i > 65535)
                value = Integer.valueOf(65535);
        } else if (name.equals(SCTP_FRAGMENT_INTERLEAVE)) {
            int i = ((Integer)value).intValue();
            if (i < 0 || i > 2)
                throw new IllegalArgumentException(
                        "Invalid value for SCTP_FRAGMENT_INTERLEAVE");
        }

        int arg;
        if (type == Integer.class) {
            arg = ((Integer)value).intValue();
        } else {
            boolean b = ((Boolean)value).booleanValue();
            arg = (b) ? 1 : 0;
        }

        setIntOption0(fd, ((SctpStdSocketOption)name).constValue(), arg);
    }

235
    static Object getIntOption(int fd, SctpSocketOption<?> name)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
            throws IOException {
        Class<?> type = name.type();

        if (type != Integer.class && type != Boolean.class)
            throw new AssertionError("Should not reach here");

        if (!(name instanceof SctpStdSocketOption))
            throw new AssertionError("Should not reach here");

        int value = getIntOption0(fd,
                ((SctpStdSocketOption)name).constValue());

        if (type == Integer.class) {
            return Integer.valueOf(value);
        } else {
            return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
        }
    }

    static void shutdown(int fd, int assocId)
            throws IOException {
        shutdown0(fd, assocId);
    }

260 261 262 263 264
    static FileDescriptor branch(int fd, int assocId) throws IOException {
        int nativefd = branch0(fd, assocId);
        return IOUtil.newFD(nativefd);
    }

265 266 267
    /* Native Methods */
    static native int socket0(boolean oneToOne) throws IOException;

268 269 270 271 272 273 274 275 276
    static native void listen0(int fd, int backlog) throws IOException;

    static native int connect0(int fd, InetAddress remote, int remotePort)
        throws IOException;

    static native void close0(int fd) throws IOException;

    static native void preClose0(int fd) throws IOException;

277 278 279 280 281 282 283 284 285 286 287 288 289
    static native void bindx(int fd, InetAddress[] addrs, int port, int length,
            boolean add, boolean preferIPv6) throws IOException;

    static native int getIntOption0(int fd, int opt) throws IOException;

    static native void setIntOption0(int fd, int opt, int arg)
        throws IOException;

    static native SocketAddress[] getLocalAddresses0(int fd) throws IOException;

    static native SocketAddress[] getRemoteAddresses0(int fd, int assocId)
            throws IOException;

290 291
    static native int branch0(int fd, int assocId) throws IOException;

292 293 294 295
    static native void setPrimAddrOption0(int fd, int assocId, InetAddress ia,
            int port) throws IOException;

    static native void setPeerPrimAddrOption0(int fd, int assocId,
296
            InetAddress ia, int port, boolean preferIPv6) throws IOException;
297 298 299 300 301 302 303 304 305 306 307

    static native SocketAddress getPrimAddrOption0(int fd, int assocId)
            throws IOException;

    /* retVals [0] maxInStreams, [1] maxOutStreams */
    static native void getInitMsgOption0(int fd, int[] retVals) throws IOException;

    static native void setInitMsgOption0(int fd, int arg1, int arg2)
            throws IOException;

    static native void shutdown0(int fd, int assocId);
308 309 310 311 312 313

    static native void init();

    static {
        init();
    }
314 315
}