src/jdk.net/share/classes/jdk/net/Sockets.java
author vtewari
Wed, 30 May 2018 16:36:35 +0530
changeset 50303 7164c3bb55df
parent 47733 fbfe06b70e16
permissions -rw-r--r--
8194298: Add support for per Socket configuration of TCP keepalive Reviewed-by: chegar, clanger, igerasim, alanb
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) 2016, 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
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    28
import java.net.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    29
import java.io.IOException;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    30
import java.util.Collections;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    31
import java.util.HashMap;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    32
import java.util.HashSet;
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    33
import java.util.Map;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    34
import java.util.Set;
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    35
import jdk.net.ExtendedSocketOptions.PlatformSocketOptions;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    36
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    37
/**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    38
 * Defines static methods to set and get socket options defined by the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    39
 * {@link java.net.SocketOption} interface. All of the standard options defined
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    40
 * by {@link java.net.Socket}, {@link java.net.ServerSocket}, and
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    41
 * {@link java.net.DatagramSocket} can be set this way, as well as additional
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    42
 * or platform specific options supported by each socket type.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    43
 * <p>
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    44
 * The {@link #supportedOptions(Class)} method can be called to determine
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    45
 * the complete set of options available (per socket type) on the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    46
 * current system.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    47
 * <p>
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    48
 * When a security manager is installed, some non-standard socket options
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    49
 * may require a security permission before being set or get.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    50
 * The details are specified in {@link ExtendedSocketOptions}. No permission
24708
13efc72a09bd 8044590: Broken links in jre.api.net.socketoptions
chegar
parents: 23879
diff changeset
    51
 * is required for {@link java.net.StandardSocketOptions}.
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    52
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    53
 * @see java.nio.channels.NetworkChannel
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
public class Sockets {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    56
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    57
    private static final Map<Class<?>,Set<SocketOption<?>>>
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
    58
            options = optionSets();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    59
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    60
    private Sockets() {}
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
     * Sets the value of a socket option on a {@link java.net.Socket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    64
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    65
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    66
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    67
     * @param value The value of the socket option. May be null for some
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    68
     *              options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    69
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    70
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    71
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    72
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    73
     * @throws IllegalArgumentException if the value is not valid for
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    74
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    75
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    76
     * @throws IOException if an I/O error occurs, or socket is closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    77
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    78
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    79
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    80
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    81
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    82
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    83
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    84
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    85
    public static <T> void setOption(Socket s, SocketOption<T> name, T value) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    86
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    87
        s.setOption(name, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    88
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    89
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    90
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    91
     * Returns the value of a socket option from a {@link java.net.Socket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    92
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    93
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    94
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    95
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    96
     * @return The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    97
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    98
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    99
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   100
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   101
     * @throws IOException if an I/O error occurs
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   102
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   103
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   104
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   105
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   106
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   107
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   108
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   109
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   110
    public static <T> T getOption(Socket s, SocketOption<T> name) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   111
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   112
        return s.getOption(name);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   113
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   114
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   115
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   116
     * Sets the value of a socket option on a {@link java.net.ServerSocket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   117
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   118
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   119
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   120
     * @param value The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   121
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   122
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   123
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   124
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   125
     * @throws IllegalArgumentException if the value is not valid for
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   126
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   127
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   128
     * @throws IOException if an I/O error occurs
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   129
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   130
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   131
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   132
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   133
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   134
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   135
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   136
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   137
    public static <T> void setOption(ServerSocket s, SocketOption<T> name, T value) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   138
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   139
        s.setOption(name, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   140
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   141
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   142
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   143
     * Returns the value of a socket option from a {@link java.net.ServerSocket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   144
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   145
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   146
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   147
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   148
     * @return The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   149
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   150
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   151
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   152
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   153
     * @throws IOException if an I/O error occurs
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   154
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   155
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   156
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   157
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   158
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   159
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   160
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   161
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   162
    public static <T> T getOption(ServerSocket s, SocketOption<T> name) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   163
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   164
        return s.getOption(name);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   165
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   166
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   167
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   168
     * Sets the value of a socket option on a {@link java.net.DatagramSocket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   169
     * or {@link java.net.MulticastSocket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   170
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   171
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   172
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   173
     * @param value The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   174
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   175
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   176
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   177
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   178
     * @throws IllegalArgumentException if the value is not valid for
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   179
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   180
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   181
     * @throws IOException if an I/O error occurs
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   182
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   183
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   184
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   185
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   186
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   187
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   188
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   189
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   190
    public static <T> void setOption(DatagramSocket s, SocketOption<T> name, T value) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   191
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   192
        s.setOption(name, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   193
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   194
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   195
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   196
     * Returns the value of a socket option from a
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   197
     * {@link java.net.DatagramSocket} or {@link java.net.MulticastSocket}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   198
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   199
     * @param s the socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   200
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   201
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   202
     * @return The value of the socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   203
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   204
     * @throws UnsupportedOperationException if the socket does not support
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   205
     *         the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   206
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   207
     * @throws IOException if an I/O error occurs
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   208
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   209
     * @throws NullPointerException if name is null
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   210
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   211
     * @throws SecurityException if a security manager is set and the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   212
     *         caller does not have any required permission.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   213
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   214
     * @see java.net.StandardSocketOptions
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   215
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   216
    public static <T> T getOption(DatagramSocket s, SocketOption<T> name) throws IOException
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   217
    {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   218
        return s.getOption(name);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   219
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   220
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   221
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   222
     * Returns a set of {@link java.net.SocketOption}s supported by the
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   223
     * given socket type. This set may include standard options and also
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   224
     * non standard extended options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   225
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   226
     * @param socketType the type of java.net socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   227
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   228
     * @throws IllegalArgumentException if socketType is not a valid
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   229
     *         socket type from the java.net package.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   230
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   231
    public static Set<SocketOption<?>> supportedOptions(Class<?> socketType) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   232
        Set<SocketOption<?>> set = options.get(socketType);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   233
        if (set == null) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   234
            throw new IllegalArgumentException("unknown socket type");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   235
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   236
        return set;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   237
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   238
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   239
    private static void checkValueType(Object value, Class<?> type) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   240
        if (!type.isAssignableFrom(value.getClass())) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   241
            String s = "Found: " + value.getClass().toString() + " Expected: "
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   242
                        + type.toString();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   243
            throw new IllegalArgumentException(s);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   244
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   245
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   246
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   247
    private static volatile boolean checkedReusePort;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   248
    private static volatile boolean isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   249
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   250
    /**
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   251
     * Tells whether SO_REUSEPORT is supported.
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   252
     */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   253
    static boolean isReusePortAvailable() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   254
        if (!checkedReusePort) {
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   255
            Set<SocketOption<?>> s = new Socket().supportedOptions();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   256
            isReusePortAvailable = s.contains(StandardSocketOptions.SO_REUSEPORT);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   257
            checkedReusePort = true;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   258
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   259
        return isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   260
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   261
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   262
    private static Map<Class<?>,Set<SocketOption<?>>> optionSets() {
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   263
        Map<Class<?>,Set<SocketOption<?>>> options = new HashMap<>();
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   264
        boolean flowsupported = PlatformSocketOptions.get().flowSupported();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   265
        boolean reuseportsupported = isReusePortAvailable();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   266
        // Socket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   267
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   268
        Set<SocketOption<?>> set = new HashSet<>();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   269
        set.add(StandardSocketOptions.SO_KEEPALIVE);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   270
        set.add(StandardSocketOptions.SO_SNDBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   271
        set.add(StandardSocketOptions.SO_RCVBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   272
        set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   273
        if (reuseportsupported) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   274
            set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   275
        }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   276
        set.add(StandardSocketOptions.SO_LINGER);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   277
        set.add(StandardSocketOptions.IP_TOS);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   278
        set.add(StandardSocketOptions.TCP_NODELAY);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   279
        if (flowsupported) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   280
            set.add(ExtendedSocketOptions.SO_FLOW_SLA);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   281
        }
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   282
        if (QuickAck.available) {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   283
            set.add(ExtendedSocketOptions.TCP_QUICKACK);
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   284
        }
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   285
        if (KeepAliveOptions.AVAILABLE) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   286
            set.addAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   287
                    ExtendedSocketOptions.TCP_KEEPIDLE,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   288
                    ExtendedSocketOptions.TCP_KEEPINTERVAL));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   289
        }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   290
        set = Collections.unmodifiableSet(set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   291
        options.put(Socket.class, set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   292
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   293
        // ServerSocket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   294
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   295
        set = new HashSet<>();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   296
        set.add(StandardSocketOptions.SO_RCVBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   297
        set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   298
        if (reuseportsupported) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   299
            set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   300
        }
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   301
        if (QuickAck.available) {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   302
            set.add(ExtendedSocketOptions.TCP_QUICKACK);
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   303
        }
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   304
        if (KeepAliveOptions.AVAILABLE) {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   305
            set.addAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   306
                    ExtendedSocketOptions.TCP_KEEPIDLE,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   307
                    ExtendedSocketOptions.TCP_KEEPINTERVAL));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   308
        }
27341
9a5b45530ed4 8062744: jdk.net.Sockets.setOption/getOption does not support IP_TOS
michaelm
parents: 25859
diff changeset
   309
        set.add(StandardSocketOptions.IP_TOS);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   310
        set = Collections.unmodifiableSet(set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   311
        options.put(ServerSocket.class, set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   312
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   313
        // DatagramSocket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   314
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   315
        set = new HashSet<>();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   316
        set.add(StandardSocketOptions.SO_SNDBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   317
        set.add(StandardSocketOptions.SO_RCVBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   318
        set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   319
        if (reuseportsupported) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   320
            set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   321
        }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   322
        set.add(StandardSocketOptions.IP_TOS);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   323
        if (flowsupported) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   324
            set.add(ExtendedSocketOptions.SO_FLOW_SLA);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   325
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   326
        set = Collections.unmodifiableSet(set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   327
        options.put(DatagramSocket.class, set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   328
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   329
        // MulticastSocket
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   330
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   331
        set = new HashSet<>();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   332
        set.add(StandardSocketOptions.SO_SNDBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   333
        set.add(StandardSocketOptions.SO_RCVBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   334
        set.add(StandardSocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   335
        if (reuseportsupported) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   336
            set.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 34894
diff changeset
   337
        }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   338
        set.add(StandardSocketOptions.IP_TOS);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   339
        set.add(StandardSocketOptions.IP_MULTICAST_IF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   340
        set.add(StandardSocketOptions.IP_MULTICAST_TTL);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   341
        set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   342
        if (flowsupported) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   343
            set.add(ExtendedSocketOptions.SO_FLOW_SLA);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   344
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   345
        set = Collections.unmodifiableSet(set);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   346
        options.put(MulticastSocket.class, set);
37676
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   347
24ef455da1b0 8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents: 36115
diff changeset
   348
        return Collections.unmodifiableMap(options);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   349
    }
47733
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   350
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   351
    /**
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   352
     * Tells whether TCP_QUICKACK is supported.
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   353
     */
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   354
    static class QuickAck {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   355
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   356
        static final boolean available;
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   357
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   358
        static {
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   359
            Set<SocketOption<?>> s = new Socket().supportedOptions();
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   360
            available = s.contains(ExtendedSocketOptions.TCP_QUICKACK);
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   361
        }
fbfe06b70e16 8145635: Add TCP_QUICKACK socket option
vtewari
parents: 47216
diff changeset
   362
    }
50303
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   363
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   364
    /**
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   365
     * Tells whether TCP_KEEPALIVE options are supported.
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   366
     */
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   367
    static class KeepAliveOptions {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   368
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   369
        static final boolean AVAILABLE;
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   370
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   371
        static {
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   372
            Set<SocketOption<?>> s = new Socket().supportedOptions();
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   373
            AVAILABLE = s.containsAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   374
                                            ExtendedSocketOptions.TCP_KEEPIDLE,
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   375
                                            ExtendedSocketOptions.TCP_KEEPINTERVAL));
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   376
        }
7164c3bb55df 8194298: Add support for per Socket configuration of TCP keepalive
vtewari
parents: 47733
diff changeset
   377
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   378
}