src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
author michaelm
Thu, 13 Jun 2019 09:10:51 +0100
changeset 55375 96c7427456f9
parent 55081 dd321e3596c0
child 55693 9a97b1393e72
permissions -rw-r--r--
8216417: cleanup of IPv6 scope-id handling Reviewed-by: alanb, chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    29
import java.util.Collections;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    30
import java.util.HashSet;
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
    31
import java.util.Objects;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    32
import java.util.Set;
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    33
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    34
import sun.net.ResourceManager;
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
    35
import sun.net.ext.ExtendedSocketOptions;
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
    36
import sun.net.util.IPAddressUtil;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36115
diff changeset
    37
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Abstract datagram and multicast socket implementation base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Note: This is not a public class, so that applets cannot call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * into the implementation directly and hence cannot bypass the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * security checks present in the DatagramSocket and MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* timeout value for receive() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int trafficClass = 0;
10693
6e9ebed2e783 7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents: 10596
diff changeset
    55
    protected InetAddress connectedAddress = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    58
    private static final String os =
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    59
            GetPropertyAction.privilegedGetProperty("os.name");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    60
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    61
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    62
     * flag set if the native connect() call not to be used
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    63
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32503
diff changeset
    64
    private static final boolean connectDisabled = os.contains("OS X");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    65
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Load net library into runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27078
diff changeset
    71
            new java.security.PrivilegedAction<>() {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    72
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    73
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    74
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    75
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    76
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    79
    private static volatile boolean checkedReusePort;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    80
    private static volatile boolean isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    81
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    82
    /**
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    83
     * Tells whether SO_REUSEPORT is supported.
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    84
     */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    85
    static boolean isReusePortAvailable() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    86
        if (!checkedReusePort) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    87
            isReusePortAvailable = isReusePortAvailable0();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    88
            checkedReusePort = true;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    89
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    90
        return isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    91
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    92
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    93
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates a datagram socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected synchronized void create() throws SocketException {
10594
d6723ee39ebf 7073491: -Dsun.net.maxDatagramSockets=1 does not work correctly with system.gc()
michaelm
parents: 9035
diff changeset
    97
        ResourceManager.beforeUdpCreate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        fd = new FileDescriptor();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    99
        try {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   100
            datagramSocketCreate();
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
   101
            SocketCleanable.register(fd);
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   102
        } catch (SocketException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   103
            ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   104
            fd = null;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   105
            throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   106
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Binds a datagram socket to a local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected synchronized void bind(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        throws SocketException {
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   114
        if (laddr.isLinkLocalAddress()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   115
            laddr = IPAddressUtil.toScopedAddress(laddr);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   116
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        bind0(lport, laddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected abstract void bind0(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Sends a datagram packet. The packet contains the data and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * destination address to send the packet to.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   126
     * @param p the packet to be sent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   128
    protected void send(DatagramPacket p) throws IOException {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   129
        InetAddress orig = p.getAddress();
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   130
        if (orig.isLinkLocalAddress()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   131
            InetAddress scoped = IPAddressUtil.toScopedAddress(orig);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   132
            if (orig != scoped) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   133
                p = new DatagramPacket(p.getData(), p.getOffset(),
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   134
                                       p.getLength(), scoped, p.getPort());
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   135
            }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   136
        }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   137
        send0(p);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   138
    }
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   139
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   140
    protected abstract void send0(DatagramPacket p) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Connects a datagram socket to a remote destination. This associates the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * address with the local socket so that datagrams may only be sent to this destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * and received from this destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param address the remote InetAddress to connect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param port the remote port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected void connect(InetAddress address, int port) throws SocketException {
55375
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   150
        if (address.isLinkLocalAddress()) {
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   151
            address = IPAddressUtil.toScopedAddress(address);
96c7427456f9 8216417: cleanup of IPv6 scope-id handling
michaelm
parents: 55081
diff changeset
   152
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        connect0(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        connectedAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        connectedPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Disconnects a previously connected socket. Does nothing if the socket was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * not connected already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    protected void disconnect() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14014
diff changeset
   164
        disconnect0(connectedAddress.holder().getFamily());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Peek at the packet to see who it is from.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   172
     * @param i the address to populate with the sender address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected abstract int peek(InetAddress i) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected abstract int peekData(DatagramPacket p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Receive the datagram packet.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   178
     * @param p the packet to receive into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected synchronized void receive(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        receive0(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected abstract void receive0(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Set the TTL (time-to-live) option.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   190
     * @param ttl TTL to be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    protected abstract void setTimeToLive(int ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected abstract int getTimeToLive() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Set the TTL (time-to-live) option.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   201
     * @param ttl TTL to be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10594
diff changeset
   203
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected abstract void setTTL(byte ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10594
diff changeset
   209
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected abstract byte getTTL() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Join the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   214
     * @param inetaddr multicast address to join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected void join(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        join(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Leave the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   222
     * @param inetaddr multicast address to leave.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    protected void leave(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        leave(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Join the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   229
     * @param mcastaddr multicast address to join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *        datagram packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Leave the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   249
     * @param mcastaddr  multicast address to leave.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param netIf specified the local interface to leave the group at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Close the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    protected void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (fd != null) {
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
   270
            SocketCleanable.unregister(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            datagramSocketClose();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   272
            ResourceManager.afterUdpClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    protected boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return (fd == null) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * set a value - since we only support (setting) binary options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * here, o must be a Boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     public void setOption(int optID, Object o) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            /* check type safety b4 going native.  These should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
             * fail, since only java.Socket* has access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             * PlainSocketImpl.setOption().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                 throw new SocketException("bad argument for SO_TIMEOUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             int tmp = ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
             if (tmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                 throw new IllegalArgumentException("timeout < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
             timeout = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                 throw new SocketException("bad argument for IP_TOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             trafficClass = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                 throw new SocketException("bad argument for SO_REUSEADDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                 throw new SocketException("bad argument for SO_BROADCAST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             throw new SocketException("Cannot re-bind Socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             if (o == null || !(o instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                 ((Integer)o).intValue() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                 throw new SocketException("bad argument for SO_SNDBUF or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                           "SO_RCVBUF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             if (o == null || !(o instanceof InetAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                 throw new SocketException("bad argument for IP_MULTICAST_IF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
             if (o == null || !(o instanceof NetworkInterface))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                 throw new SocketException("bad argument for IP_MULTICAST_IF2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
             if (o == null || !(o instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                 throw new SocketException("bad argument for IP_MULTICAST_LOOP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
             break;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   342
         case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   343
             if (o == null || !(o instanceof Boolean)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   344
                 throw new SocketException("bad argument for SO_REUSEPORT");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   345
             }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   346
             if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   347
                 throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   348
             }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   349
             break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
             throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         socketSetOption(optID, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * get option's state - set or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public Object getOption(int optID) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        Object result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            case SO_TIMEOUT:
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   369
                result = timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if ( ((Integer)result).intValue() == -1) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   375
                    result = trafficClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   390
            case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   391
                if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   392
                    throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   393
                }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   394
                result = socketGetOption(optID);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   395
                break;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   396
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   404
    static final ExtendedSocketOptions extendedOptions =
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   405
            ExtendedSocketOptions.getInstance();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   406
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   407
    private static final Set<SocketOption<?>> datagramSocketOptions = datagramSocketOptions();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   408
    private static final Set<SocketOption<?>> multicastSocketOptions = multicastSocketOptions();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   409
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   410
    private static Set<SocketOption<?>> datagramSocketOptions() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   411
        HashSet<SocketOption<?>> options = new HashSet<>();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   412
        options.add(StandardSocketOptions.SO_SNDBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   413
        options.add(StandardSocketOptions.SO_RCVBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   414
        options.add(StandardSocketOptions.SO_REUSEADDR);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   415
        options.add(StandardSocketOptions.IP_TOS);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   416
        if (isReusePortAvailable())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   417
            options.add(StandardSocketOptions.SO_REUSEPORT);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   418
        options.addAll(ExtendedSocketOptions.datagramSocketOptions());
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   419
        return Collections.unmodifiableSet(options);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   420
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   421
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   422
    private static Set<SocketOption<?>> multicastSocketOptions() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   423
        HashSet<SocketOption<?>> options = new HashSet<>();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   424
        options.add(StandardSocketOptions.SO_SNDBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   425
        options.add(StandardSocketOptions.SO_RCVBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   426
        options.add(StandardSocketOptions.SO_REUSEADDR);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   427
        options.add(StandardSocketOptions.IP_TOS);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   428
        options.add(StandardSocketOptions.IP_MULTICAST_IF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   429
        options.add(StandardSocketOptions.IP_MULTICAST_TTL);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   430
        options.add(StandardSocketOptions.IP_MULTICAST_LOOP);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   431
        if (isReusePortAvailable())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   432
            options.add(StandardSocketOptions.SO_REUSEPORT);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   433
        options.addAll(ExtendedSocketOptions.datagramSocketOptions());
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   434
        return Collections.unmodifiableSet(options);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   435
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   436
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   437
    @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   438
    protected Set<SocketOption<?>> supportedOptions() {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   439
        if (getDatagramSocket() instanceof MulticastSocket)
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   440
            return multicastSocketOptions;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   441
        else
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   442
            return datagramSocketOptions;
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   443
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   444
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   445
    @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   446
    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   447
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   448
        if (!supportedOptions().contains(name))
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   449
            throw new UnsupportedOperationException("'" + name + "' not supported");
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   450
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   451
        if (!name.type().isInstance(value))
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   452
            throw new IllegalArgumentException("Invalid value '" + value + "'");
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   453
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   454
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   455
            throw new SocketException("Socket closed");
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   456
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   457
        if (name == StandardSocketOptions.SO_SNDBUF) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   458
            if (((Integer)value).intValue() < 0)
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   459
                throw new IllegalArgumentException("Invalid send buffer size:" + value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   460
            setOption(SocketOptions.SO_SNDBUF, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   461
        } else if (name == StandardSocketOptions.SO_RCVBUF) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   462
            if (((Integer)value).intValue() < 0)
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   463
                throw new IllegalArgumentException("Invalid recv buffer size:" + value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   464
            setOption(SocketOptions.SO_RCVBUF, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   465
        } else if (name == StandardSocketOptions.SO_REUSEADDR) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   466
            setOption(SocketOptions.SO_REUSEADDR, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   467
        } else if (name == StandardSocketOptions.SO_REUSEPORT) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   468
            setOption(SocketOptions.SO_REUSEPORT, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   469
        } else if (name == StandardSocketOptions.IP_TOS) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   470
            int i = ((Integer)value).intValue();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   471
            if (i < 0 || i > 255)
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   472
                throw new IllegalArgumentException("Invalid IP_TOS value: " + value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   473
            setOption(SocketOptions.IP_TOS, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   474
        } else if (name == StandardSocketOptions.IP_MULTICAST_IF ) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   475
            setOption(SocketOptions.IP_MULTICAST_IF2, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   476
        } else if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   477
            int i = ((Integer)value).intValue();
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   478
            if (i < 0 || i > 255)
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   479
                throw new IllegalArgumentException("Invalid TTL/hop value: " + value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   480
            setTimeToLive((Integer)value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   481
        } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   482
            setOption(SocketOptions.IP_MULTICAST_LOOP, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   483
        } else if (extendedOptions.isOptionSupported(name)) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   484
            extendedOptions.setOption(fd, name, value);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   485
        } else {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   486
            throw new AssertionError("unknown option :" + name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   487
        }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   488
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   489
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   490
    @Override
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   491
    @SuppressWarnings("unchecked")
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   492
    protected <T> T getOption(SocketOption<T> name) throws IOException {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   493
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   494
        if (!supportedOptions().contains(name))
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   495
            throw new UnsupportedOperationException("'" + name + "' not supported");
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   496
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   497
        if (isClosed())
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   498
            throw new SocketException("Socket closed");
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   499
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   500
        if (name == StandardSocketOptions.SO_SNDBUF) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   501
            return (T) getOption(SocketOptions.SO_SNDBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   502
        } else if (name == StandardSocketOptions.SO_RCVBUF) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   503
            return (T) getOption(SocketOptions.SO_RCVBUF);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   504
        } else if (name == StandardSocketOptions.SO_REUSEADDR) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   505
            return (T) getOption(SocketOptions.SO_REUSEADDR);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   506
        } else if (name == StandardSocketOptions.SO_REUSEPORT) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   507
            return (T) getOption(SocketOptions.SO_REUSEPORT);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   508
        } else if (name == StandardSocketOptions.IP_TOS) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   509
            return (T) getOption(SocketOptions.IP_TOS);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   510
        } else if (name == StandardSocketOptions.IP_MULTICAST_IF) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   511
            return (T) getOption(SocketOptions.IP_MULTICAST_IF2);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   512
        } else if (name == StandardSocketOptions.IP_MULTICAST_TTL) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   513
            return (T) ((Integer) getTimeToLive());
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   514
        } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   515
            return (T) getOption(SocketOptions.IP_MULTICAST_LOOP);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   516
        } else if (extendedOptions.isOptionSupported(name)) {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   517
            return (T) extendedOptions.getOption(fd, name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   518
        } else {
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   519
            throw new AssertionError("unknown option: " + name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   520
        }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   521
    }
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 48737
diff changeset
   522
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    protected abstract void datagramSocketCreate() throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    protected abstract void datagramSocketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    protected abstract void socketSetOption(int opt, Object val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    protected abstract Object socketGetOption(int opt) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    protected abstract void connect0(InetAddress address, int port) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected abstract void disconnect0(int family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   532
    protected boolean nativeConnectDisabled() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   533
        return connectDisabled;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   534
    }
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   535
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 29986
diff changeset
   536
    abstract int dataAvailable();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   537
    private static native boolean isReusePortAvailable0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
}