src/jdk.net/share/classes/jdk/net/ExtendedSocketOptions.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 51981 48dec0c13bec
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     1
/*
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     4
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    10
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    15
 * accompanied this code).
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    16
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    20
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    23
 * questions.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    24
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    25
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    26
package jdk.net;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    27
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    28
import java.io.FileDescriptor;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    29
import java.net.SocketException;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    30
import java.net.SocketOption;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    31
import java.security.AccessController;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    32
import java.security.PrivilegedAction;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    33
import java.util.Collections;
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
    34
import java.util.HashSet;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    35
import java.util.Set;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51981
diff changeset
    36
import jdk.internal.access.JavaIOFileDescriptorAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51981
diff changeset
    37
import jdk.internal.access.SharedSecrets;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    38
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    39
/**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    40
 * Defines extended socket options, beyond those defined in
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    41
 * {@link java.net.StandardSocketOptions}. These options may be platform
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    42
 * specific.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    43
 *
24713
5451c8499988 8044766: New jdk.net classes have @since 1.9 tags in 8u20
michaelm
parents: 23879
diff changeset
    44
 * @since 1.8
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    45
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    46
public final class ExtendedSocketOptions {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    47
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    48
    private static class ExtSocketOption<T> implements SocketOption<T> {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    49
        private final String name;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    50
        private final Class<T> type;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    51
        ExtSocketOption(String name, Class<T> type) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    52
            this.name = name;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    53
            this.type = type;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    54
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    55
        @Override public String name() { return name; }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    56
        @Override public Class<T> type() { return type; }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    57
        @Override public String toString() { return name; }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    58
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    59
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    60
    private ExtendedSocketOptions() { }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    61
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    62
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    63
     * Service level properties. When a security manager is installed,
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    64
     * setting or getting this option requires a {@link NetworkPermission}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    65
     * {@code ("setOption.SO_FLOW_SLA")} or {@code "getOption.SO_FLOW_SLA"}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    66
     * respectively.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    67
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    68
    public static final SocketOption<SocketFlow> SO_FLOW_SLA = new
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    69
        ExtSocketOption<SocketFlow>("SO_FLOW_SLA", SocketFlow.class);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    70
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    71
    /**
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    72
     * Disable Delayed Acknowledgements.
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    73
     *
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    74
     * <p>
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    75
     * This socket option can be used to reduce or disable delayed
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    76
     * acknowledgments (ACKs). When {@code TCP_QUICKACK} is enabled, ACKs are
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    77
     * sent immediately, rather than delayed if needed in accordance to normal
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    78
     * TCP operation. This option is not permanent, it only enables a switch to
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    79
     * or from {@code TCP_QUICKACK} mode. Subsequent operations of the TCP
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    80
     * protocol will once again disable/enable {@code TCP_QUICKACK} mode
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    81
     * depending on internal protocol processing and factors such as delayed ACK
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    82
     * timeouts occurring and data transfer, therefore this option needs to be
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    83
     * set with {@code setOption} after each operation of TCP on a given socket.
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    84
     *
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    85
     * <p>
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    86
     * The value of this socket option is a {@code Boolean} that represents
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    87
     * whether the option is enabled or disabled. The socket option is specific
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    88
     * to stream-oriented sockets using the TCP/IP protocol. The exact semantics
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    89
     * of this socket option are socket type and system dependent.
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    90
     *
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    91
     * @since 10
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    92
     */
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    93
    public static final SocketOption<Boolean> TCP_QUICKACK =
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
    94
            new ExtSocketOption<Boolean>("TCP_QUICKACK", Boolean.class);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
    95
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
    96
    /**
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
    97
     * Keep-Alive idle time.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
    98
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
    99
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   100
     * The value of this socket option is an {@code Integer} that is the number
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   101
     * of seconds of idle time before keep-alive initiates a probe. The socket
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   102
     * option is specific to stream-oriented sockets using the TCP/IP protocol.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   103
     * The exact semantics of this socket option are system dependent.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   104
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   105
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   106
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   107
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   108
     * idle for some amount of time. The default value for this idle period is
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   109
     * system dependent, but is typically 2 hours. The {@code TCP_KEEPIDLE}
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   110
     * option can be used to affect this value for a given socket.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   111
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   112
     * @since 11
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   113
     */
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   114
    public static final SocketOption<Integer> TCP_KEEPIDLE
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   115
            = new ExtSocketOption<Integer>("TCP_KEEPIDLE", Integer.class);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   116
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   117
    /**
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   118
     * Keep-Alive retransmission interval time.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   119
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   120
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   121
     * The value of this socket option is an {@code Integer} that is the number
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   122
     * of seconds to wait before retransmitting a keep-alive probe. The socket
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   123
     * option is specific to stream-oriented sockets using the TCP/IP protocol.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   124
     * The exact semantics of this socket option are system dependent.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   125
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   126
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   127
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   128
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   129
     * idle for some amount of time. If the remote system does not respond to a
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   130
     * keep-alive probe, TCP retransmits the probe after some amount of time.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   131
     * The default value for this retransmission interval is system dependent,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   132
     * but is typically 75 seconds. The {@code TCP_KEEPINTERVAL} option can be
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   133
     * used to affect this value for a given socket.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   134
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   135
     * @since 11
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   136
     */
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   137
    public static final SocketOption<Integer> TCP_KEEPINTERVAL
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   138
            = new ExtSocketOption<Integer>("TCP_KEEPINTERVAL", Integer.class);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   139
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   140
    /**
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   141
     * Keep-Alive retransmission maximum limit.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   142
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   143
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   144
     * The value of this socket option is an {@code Integer} that is the maximum
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   145
     * number of keep-alive probes to be sent. The socket option is specific to
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   146
     * stream-oriented sockets using the TCP/IP protocol. The exact semantics of
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   147
     * this socket option are system dependent.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   148
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   149
     * <p>
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   150
     * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   151
     * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   152
     * idle for some amount of time. If the remote system does not respond to a
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   153
     * keep-alive probe, TCP retransmits the probe a certain number of times
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   154
     * before a connection is considered to be broken. The default value for
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   155
     * this keep-alive probe retransmit limit is system dependent, but is
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   156
     * typically 8. The {@code TCP_KEEPCOUNT} option can be used to affect this
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   157
     * value for a given socket.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   158
     *
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   159
     * @since 11
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   160
     */
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   161
    public static final SocketOption<Integer> TCP_KEEPCOUNT
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   162
            = new ExtSocketOption<Integer>("TCP_KEEPCOUNT", Integer.class);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   163
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   164
    private static final PlatformSocketOptions platformSocketOptions =
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   165
            PlatformSocketOptions.get();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   166
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   167
    private static final boolean flowSupported =
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   168
            platformSocketOptions.flowSupported();
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   169
    private static final boolean quickAckSupported =
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   170
            platformSocketOptions.quickAckSupported();
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   171
    private static final boolean keepAliveOptSupported =
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   172
            platformSocketOptions.keepAliveOptionsSupported();
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   173
    private static final Set<SocketOption<?>> extendedOptions = options();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   174
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   175
    static Set<SocketOption<?>> options() {
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   176
        Set<SocketOption<?>> options = new HashSet<>();
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   177
        if (flowSupported) {
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   178
            options.add(SO_FLOW_SLA);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   179
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   180
        if (quickAckSupported) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   181
            options.add(TCP_QUICKACK);
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   182
        }
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   183
        if (keepAliveOptSupported) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   184
            options.addAll(Set.of(TCP_KEEPCOUNT, TCP_KEEPIDLE, TCP_KEEPINTERVAL));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   185
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   186
        return Collections.unmodifiableSet(options);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   187
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   188
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   189
    static {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   190
        // Registers the extended socket options with the base module.
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   191
        sun.net.ext.ExtendedSocketOptions.register(
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   192
                new sun.net.ext.ExtendedSocketOptions(extendedOptions) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   193
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   194
            @Override
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   195
            public void setOption(FileDescriptor fd,
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   196
                                  SocketOption<?> option,
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   197
                                  Object value)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   198
                throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   199
            {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   200
                SecurityManager sm = System.getSecurityManager();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   201
                if (sm != null)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   202
                    sm.checkPermission(new NetworkPermission("setOption." + option.name()));
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   203
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   204
                if (fd == null || !fd.valid())
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   205
                    throw new SocketException("socket closed");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   206
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   207
                if (option == SO_FLOW_SLA) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   208
                    assert flowSupported;
51981
48dec0c13bec 8209454: [error-prone] TypeParameterUnusedInFormals in jdk.net
chegar
parents: 50303
diff changeset
   209
                    SocketFlow flow = checkValueType(value, SocketFlow.class);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   210
                    setFlowOption(fd, flow);
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   211
                } else if (option == TCP_QUICKACK) {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   212
                    setQuickAckOption(fd, (boolean) value);
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   213
                } else if (option == TCP_KEEPCOUNT) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   214
                    setTcpkeepAliveProbes(fd, (Integer) value);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   215
                } else if (option == TCP_KEEPIDLE) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   216
                    setTcpKeepAliveTime(fd, (Integer) value);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   217
                } else if (option == TCP_KEEPINTERVAL) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   218
                    setTcpKeepAliveIntvl(fd, (Integer) value);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   219
                } else {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   220
                    throw new InternalError("Unexpected option " + option);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   221
                }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   222
            }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   223
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   224
            @Override
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   225
            public Object getOption(FileDescriptor fd,
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   226
                                    SocketOption<?> option)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   227
                throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   228
            {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   229
                SecurityManager sm = System.getSecurityManager();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   230
                if (sm != null)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   231
                    sm.checkPermission(new NetworkPermission("getOption." + option.name()));
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   232
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   233
                if (fd == null || !fd.valid())
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   234
                    throw new SocketException("socket closed");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   235
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   236
                if (option == SO_FLOW_SLA) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   237
                    assert flowSupported;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   238
                    SocketFlow flow = SocketFlow.create();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   239
                    getFlowOption(fd, flow);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   240
                    return flow;
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   241
                } else if (option == TCP_QUICKACK) {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   242
                    return getQuickAckOption(fd);
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   243
                } else if (option == TCP_KEEPCOUNT) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   244
                    return getTcpkeepAliveProbes(fd);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   245
                } else if (option == TCP_KEEPIDLE) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   246
                    return getTcpKeepAliveTime(fd);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   247
                } else if (option == TCP_KEEPINTERVAL) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   248
                    return getTcpKeepAliveIntvl(fd);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   249
                } else {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   250
                    throw new InternalError("Unexpected option " + option);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   251
                }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   252
            }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   253
        });
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   254
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   255
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   256
    @SuppressWarnings("unchecked")
51981
48dec0c13bec 8209454: [error-prone] TypeParameterUnusedInFormals in jdk.net
chegar
parents: 50303
diff changeset
   257
    private static <T> T checkValueType(Object value, Class<T> type) {
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   258
        if (!type.isAssignableFrom(value.getClass())) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   259
            String s = "Found: " + value.getClass() + ", Expected: " + type;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   260
            throw new IllegalArgumentException(s);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   261
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   262
        return (T) value;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   263
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   264
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   265
    private static final JavaIOFileDescriptorAccess fdAccess =
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   266
            SharedSecrets.getJavaIOFileDescriptorAccess();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   267
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   268
    private static void setFlowOption(FileDescriptor fd, SocketFlow f)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   269
        throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   270
    {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   271
        int status = platformSocketOptions.setFlowOption(fdAccess.get(fd),
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   272
                                                         f.priority(),
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   273
                                                         f.bandwidth());
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   274
        f.status(status);  // augment the given flow with the status
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   275
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   276
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   277
    private static void getFlowOption(FileDescriptor fd, SocketFlow f)
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   278
            throws SocketException {
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   279
        int status = platformSocketOptions.getFlowOption(fdAccess.get(fd), f);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   280
        f.status(status);  // augment the given flow with the status
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   281
    }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   282
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   283
    private static void setQuickAckOption(FileDescriptor fd, boolean enable)
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   284
            throws SocketException {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   285
        platformSocketOptions.setQuickAck(fdAccess.get(fd), enable);
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   286
    }
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   287
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   288
    private static Object getQuickAckOption(FileDescriptor fd)
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   289
            throws SocketException {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   290
        return platformSocketOptions.getQuickAck(fdAccess.get(fd));
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   291
    }
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   292
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   293
    private static void setTcpkeepAliveProbes(FileDescriptor fd, int value)
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   294
            throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   295
        platformSocketOptions.setTcpkeepAliveProbes(fdAccess.get(fd), value);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   296
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   297
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   298
    private static void setTcpKeepAliveTime(FileDescriptor fd, int value)
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   299
            throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   300
        platformSocketOptions.setTcpKeepAliveTime(fdAccess.get(fd), value);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   301
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   302
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   303
    private static void setTcpKeepAliveIntvl(FileDescriptor fd, int value)
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   304
            throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   305
        platformSocketOptions.setTcpKeepAliveIntvl(fdAccess.get(fd), value);
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   306
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   307
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   308
    private static int getTcpkeepAliveProbes(FileDescriptor fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   309
        return platformSocketOptions.getTcpkeepAliveProbes(fdAccess.get(fd));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   310
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   311
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   312
    private static int getTcpKeepAliveTime(FileDescriptor fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   313
        return platformSocketOptions.getTcpKeepAliveTime(fdAccess.get(fd));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   314
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   315
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   316
    private static int getTcpKeepAliveIntvl(FileDescriptor fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   317
        return platformSocketOptions.getTcpKeepAliveIntvl(fdAccess.get(fd));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   318
    }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   319
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   320
    static class PlatformSocketOptions {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   321
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   322
        protected PlatformSocketOptions() {}
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   323
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   324
        @SuppressWarnings("unchecked")
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   325
        private static PlatformSocketOptions newInstance(String cn) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   326
            Class<PlatformSocketOptions> c;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   327
            try {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   328
                c = (Class<PlatformSocketOptions>)Class.forName(cn);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   329
                return c.getConstructor(new Class<?>[] { }).newInstance();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   330
            } catch (ReflectiveOperationException x) {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   331
                throw new AssertionError(x);
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   332
            }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   333
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   334
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   335
        private static PlatformSocketOptions create() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   336
            String osname = AccessController.doPrivileged(
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   337
                    new PrivilegedAction<String>() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   338
                        public String run() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   339
                            return System.getProperty("os.name");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   340
                        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   341
                    });
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   342
            if ("SunOS".equals(osname)) {
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   343
                return newInstance("jdk.net.SolarisSocketOptions");
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   344
            } else if ("Linux".equals(osname)) {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   345
                return newInstance("jdk.net.LinuxSocketOptions");
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   346
            } else if (osname.startsWith("Mac")) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   347
                return newInstance("jdk.net.MacOSXSocketOptions");
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   348
            } else {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   349
                return new PlatformSocketOptions();
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   350
            }
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   351
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   352
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   353
        private static final PlatformSocketOptions instance = create();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   354
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   355
        static PlatformSocketOptions get() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   356
            return instance;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   357
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   358
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   359
        int setFlowOption(int fd, int priority, long bandwidth)
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   360
            throws SocketException
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   361
        {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   362
            throw new UnsupportedOperationException("unsupported socket option");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   363
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   364
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   365
        int getFlowOption(int fd, SocketFlow f) throws SocketException {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   366
            throw new UnsupportedOperationException("unsupported socket option");
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   367
        }
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   368
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   369
        boolean flowSupported() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   370
            return false;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   371
        }
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   372
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   373
        void setQuickAck(int fd, boolean on) throws SocketException {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   374
            throw new UnsupportedOperationException("unsupported TCP_QUICKACK option");
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   375
        }
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   376
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   377
        boolean getQuickAck(int fd) throws SocketException {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   378
            throw new UnsupportedOperationException("unsupported TCP_QUICKACK option");
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   379
        }
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   380
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   381
        boolean quickAckSupported() {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   382
            return false;
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   383
        }
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   384
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   385
        boolean keepAliveOptionsSupported() {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   386
            return false;
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   387
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   388
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   389
        void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   390
            throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   391
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   392
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   393
        void setTcpKeepAliveTime(int fd, final int value) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   394
            throw new UnsupportedOperationException("unsupported TCP_KEEPIDLE option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   395
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   396
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   397
        void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   398
            throw new UnsupportedOperationException("unsupported TCP_KEEPINTVL option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   399
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   400
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   401
        int getTcpkeepAliveProbes(int fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   402
            throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   403
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   404
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   405
        int getTcpKeepAliveTime(int fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   406
            throw new UnsupportedOperationException("unsupported TCP_KEEPIDLE option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   407
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   408
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   409
        int getTcpKeepAliveIntvl(int fd) throws SocketException {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   410
            throw new UnsupportedOperationException("unsupported TCP_KEEPINTVL option");
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   411
        }
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 34894
diff changeset
   412
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   413
}