src/jdk.net/linux/classes/jdk/internal/net/rdma/RdmaNet.java
author bpb
Wed, 06 Feb 2019 12:48:01 -0800
branchrsocket-branch
changeset 57156 81e4a12fd1a4
parent 57115 512e7cc6ccce
child 57160 c502c299d41e
permissions -rw-r--r--
rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57156
81e4a12fd1a4 rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
bpb
parents: 57115
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
package jdk.internal.net.rdma;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
import java.io.FileDescriptor;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
import java.net.Inet4Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
import java.net.Inet6Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
import java.net.ProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.net.SocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.net.SocketOption;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.StandardSocketOptions;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.nio.channels.UnsupportedAddressTypeException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.security.AccessController;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.security.PrivilegedAction;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.util.Enumeration;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import sun.net.ext.RdmaSocketOptions;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import sun.nio.ch.IOUtil;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import sun.nio.ch.Net;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
public class RdmaNet {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
    private RdmaNet() { }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
    static final ProtocolFamily UNSPEC = new ProtocolFamily() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
        public String name() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
            return "UNSPEC";
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
    };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
    static boolean isReusePortAvailable() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
        return false;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
    private static volatile boolean checkedRdma;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
    private static volatile boolean isRdmaAvailable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    static boolean isRdmaAvailable() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
        if (!checkedRdma) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
            isRdmaAvailable = isRdmaAvailable0();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
            checkedRdma = true;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
        return isRdmaAvailable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
    private static native boolean isRdmaAvailable0();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
    static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
        InetSocketAddress isa = Net.checkAddress(sa);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
        if (family == StandardProtocolFamily.INET) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            InetAddress addr = isa.getAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            if (!(addr instanceof Inet4Address))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
                throw new UnsupportedAddressTypeException();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
        if (family == StandardProtocolFamily.INET6) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
            InetAddress addr = isa.getAddress();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
            if (!(addr instanceof Inet6Address))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
                throw new UnsupportedAddressTypeException();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
        return isa;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
    // -- Socket options
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
    static final RdmaSocketOptions rdmaOptions =
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            RdmaSocketOptions.getInstance();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
    static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
            SocketOption<?> name, Object value) throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
        if (value == null)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
            throw new IllegalArgumentException("Invalid option value");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        Class<?> type = name.type();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
        if (rdmaOptions.isOptionSupported(name)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
            rdmaOptions.setOption(fd, name, value);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
            return;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
        if (type != Integer.class && type != Boolean.class)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
            throw new AssertionError("Should not reach here");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
        if (name == StandardSocketOptions.SO_RCVBUF ||
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
            name == StandardSocketOptions.SO_SNDBUF)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
        {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
            int i = ((Integer)value).intValue();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
            if (i < 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
                throw new IllegalArgumentException(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
                    "Invalid send/receive buffer size");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
        RdmaOptionKey key = RdmaSocketOptionRegistry.findOption(name, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
        if (key == null)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
            throw new AssertionError("Option not found");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
        int arg;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
        int maxValue = 1024 * 1024 * 1024 - 1; 
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
        if (type == Integer.class) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
            arg = ((Integer)value).intValue();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
            if (arg > maxValue)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
                arg = maxValue;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
            boolean b = ((Boolean)value).booleanValue();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
            arg = (b) ? 1 : 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
        boolean mayNeedConversion = (family == UNSPEC);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
        setIntOption0(fd, mayNeedConversion, key.level(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
                      key.name(), arg);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
    static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
            SocketOption<?> name) throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
        Class<?> type = name.type();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   143
        if (rdmaOptions.isOptionSupported(name)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   144
            return rdmaOptions.getOption(fd, name);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   145
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   146
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   147
        if (type != Integer.class && type != Boolean.class)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   148
            throw new AssertionError("Should not reach here");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   149
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   150
        RdmaOptionKey key = RdmaSocketOptionRegistry.findOption(name, family);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   151
        if (key == null)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   152
            throw new AssertionError("Option not found");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   153
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   154
        boolean mayNeedConversion = (family == UNSPEC);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   155
        int value = getIntOption0(fd, mayNeedConversion, key.level(),
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   156
                                  key.name());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   157
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   158
        if (type == Integer.class) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   159
            return Integer.valueOf(value);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   160
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   161
            return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   162
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   163
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   164
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   165
    // -- Socket operations --
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   166
    static FileDescriptor socket(ProtocolFamily family, boolean stream)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   167
            throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   168
        boolean preferIPv6 = Net.isIPv6Available() &&
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   169
                (family != StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   170
        return IOUtil.newFD(socket0(preferIPv6, stream, false));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   171
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   172
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   173
    static FileDescriptor serverSocket(ProtocolFamily family, boolean stream)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   174
            throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   175
        boolean preferIPv6 = Net.isIPv6Available() &&
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   176
                (family != StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   177
        return IOUtil.newFD(socket0(preferIPv6, stream, true));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   178
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   179
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   180
    private static native int socket0(boolean preferIPv6, boolean stream,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   181
            boolean reuse);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   182
    static void bind(ProtocolFamily family, FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   183
            InetAddress addr, int port) throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   184
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   185
        boolean preferIPv6 = Net.isIPv6Available() &&
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   186
                (family != StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   187
        bind0(fd, preferIPv6, addr, port);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   188
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   189
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   190
    private static native void bind0(FileDescriptor fd, boolean preferIPv6,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   191
            InetAddress addr, int port) throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   192
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   193
    static native void listen(FileDescriptor fd, int backlog)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   194
            throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   195
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   196
    static int connect(FileDescriptor fd, InetAddress remote, int remotePort)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   197
            throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   198
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   199
        return connect(UNSPEC, fd, remote, remotePort);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   200
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   201
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   202
    static int connect(ProtocolFamily family, FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   203
            InetAddress remote, int remotePort) throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   204
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   205
        boolean preferIPv6 = Net.isIPv6Available() &&
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   206
                (family != StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   207
        return connect0(preferIPv6, fd, remote, remotePort);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   208
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   209
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   210
    public static InetSocketAddress localAddress(FileDescriptor fd)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   211
            throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   212
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   213
        return new InetSocketAddress(localInetAddress(fd), localPort(fd));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   214
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   215
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   216
    private static native int connect0(boolean preferIPv6, FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   217
            InetAddress remote, int remotePort) throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   218
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   219
    static native void shutdown(FileDescriptor fd, int how) throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   220
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   221
    private static native int localPort(FileDescriptor fd)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   222
            throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   223
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   224
    private static native InetAddress localInetAddress(FileDescriptor fd)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   225
            throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   226
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   227
    private static native int getIntOption0(FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   228
            boolean mayNeedConversion, int level, int opt) throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   229
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   230
    private static native void setIntOption0(FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   231
            boolean mayNeedConversion, int level, int opt, int arg)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   232
            throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   233
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   234
    static native int poll(FileDescriptor fd, int events, long timeout)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   235
            throws IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   236
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   237
    public static native void configureBlocking(FileDescriptor fd,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   238
            boolean blocking); 
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   239
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   240
    private static native void initIDs();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   241
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   242
    static {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   243
        java.security.AccessController.doPrivileged(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   244
            new java.security.PrivilegedAction<>() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   245
                public Void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   246
                    System.loadLibrary("extnet");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   247
                    return null;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   248
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   249
            });
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   250
        IOUtil.load();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   251
        initIDs();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   252
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   253
}