jdk/src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
child 38477 f462865d453d
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
     2
 * Copyright (c) 1996, 2016, 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;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    29
import java.security.AccessController;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
    30
import sun.net.ResourceManager;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    31
import java.util.Set;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    32
import java.util.HashSet;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    33
import java.util.Collections;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36115
diff changeset
    34
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Abstract datagram and multicast socket implementation base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Note: This is not a public class, so that applets cannot call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * into the implementation directly and hence cannot bypass the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * security checks present in the DatagramSocket and MulticastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* timeout value for receive() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int timeout = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    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
    52
    protected InetAddress connectedAddress = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    55
    private static final String os =
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    56
            GetPropertyAction.privilegedGetProperty("os.name");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    57
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    58
    /**
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    59
     * 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
    60
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32503
diff changeset
    61
    private static final boolean connectDisabled = os.contains("OS X");
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Load net library into runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 27078
diff changeset
    68
            new java.security.PrivilegedAction<>() {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    69
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    70
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    71
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    72
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12538
diff changeset
    73
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    76
    private static volatile boolean checkedReusePort;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    77
    private static volatile boolean isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    78
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    79
    /**
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    80
     * Tells whether SO_REUSEPORT is supported.
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
    static boolean isReusePortAvailable() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    83
        if (!checkedReusePort) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    84
            isReusePortAvailable = isReusePortAvailable0();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    85
            checkedReusePort = true;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    86
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    87
        return isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    88
    }
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
    private static volatile Set<SocketOption<?>> socketOptions;
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
     * Returns a set of SocketOptions supported by this impl
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    94
     * and by this impl's socket (Socket or ServerSocket)
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    95
     *
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    96
     * @return a Set of SocketOptions
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    97
     */
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    98
    @Override
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    99
    protected Set<SocketOption<?>> supportedOptions() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   100
        Set<SocketOption<?>> options = socketOptions;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   101
        if (options == null) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   102
            if (isReusePortAvailable()) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   103
                options = new HashSet<>();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   104
                options.addAll(super.supportedOptions());
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   105
                options.add(StandardSocketOptions.SO_REUSEPORT);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   106
                options = Collections.unmodifiableSet(options);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   107
            } else {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   108
                options = super.supportedOptions();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   109
            }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   110
            socketOptions = options;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   111
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   112
        return options;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   113
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Creates a datagram socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    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
   119
        ResourceManager.beforeUdpCreate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        fd = new FileDescriptor();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   121
        try {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   122
            datagramSocketCreate();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   123
        } catch (SocketException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   124
            ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   125
            fd = null;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   126
            throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   127
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Binds a datagram socket to a local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected synchronized void bind(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        bind0(lport, laddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected abstract void bind0(int lport, InetAddress laddr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Sends a datagram packet. The packet contains the data and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * destination address to send the packet to.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   144
     * @param p the packet to be sent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected abstract void send(DatagramPacket p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Connects a datagram socket to a remote destination. This associates the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * address with the local socket so that datagrams may only be sent to this destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * and received from this destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param address the remote InetAddress to connect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param port the remote port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected void connect(InetAddress address, int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        connect0(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        connectedAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        connectedPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Disconnects a previously connected socket. Does nothing if the socket was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * not connected already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected void disconnect() {
16870
f35b2bd19761 8000724: Improve networking serialization
michaelm
parents: 14014
diff changeset
   167
        disconnect0(connectedAddress.holder().getFamily());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Peek at the packet to see who it is from.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   175
     * @param i the address to populate with the sender address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected abstract int peek(InetAddress i) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected abstract int peekData(DatagramPacket p) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Receive the datagram packet.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   181
     * @param p the packet to receive into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected synchronized void receive(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        receive0(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected abstract void receive0(DatagramPacket p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Set the TTL (time-to-live) option.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   193
     * @param ttl TTL to be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected abstract void setTimeToLive(int ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected abstract int getTimeToLive() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Set the TTL (time-to-live) option.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   204
     * @param ttl TTL to be set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10594
diff changeset
   206
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected abstract void setTTL(byte ttl) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Get the TTL (time-to-live) option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10594
diff changeset
   212
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    protected abstract byte getTTL() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Join the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   217
     * @param inetaddr multicast address to join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected void join(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        join(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Leave the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   225
     * @param inetaddr multicast address to leave.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected void leave(InetAddress inetaddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        leave(inetaddr, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Join the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   232
     * @param mcastaddr multicast address to join.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param netIf specifies the local interface to receive multicast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *        datagram packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        join(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    protected abstract void join(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Leave the multicast group.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13999
diff changeset
   252
     * @param mcastaddr  multicast address to leave.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param netIf specified the local interface to leave the group at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws  IllegalArgumentException if mcastaddr is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        leave(((InetSocketAddress)mcastaddr).getAddress(), netIf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected abstract void leave(InetAddress inetaddr, NetworkInterface netIf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Close the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    protected void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            datagramSocketClose();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 5506
diff changeset
   274
            ResourceManager.afterUdpClose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return (fd == null) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * set a value - since we only support (setting) binary options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * here, o must be a Boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     public void setOption(int optID, Object o) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
             throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            /* check type safety b4 going native.  These should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
             * fail, since only java.Socket* has access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             * PlainSocketImpl.setOption().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
         case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                 throw new SocketException("bad argument for SO_TIMEOUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
             int tmp = ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
             if (tmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                 throw new IllegalArgumentException("timeout < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             timeout = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             if (o == null || !(o instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                 throw new SocketException("bad argument for IP_TOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             trafficClass = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                 throw new SocketException("bad argument for SO_REUSEADDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
             if (o == null || !(o instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                 throw new SocketException("bad argument for SO_BROADCAST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
             throw new SocketException("Cannot re-bind Socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             if (o == null || !(o instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                 ((Integer)o).intValue() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                 throw new SocketException("bad argument for SO_SNDBUF or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                           "SO_RCVBUF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
             if (o == null || !(o instanceof InetAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                 throw new SocketException("bad argument for IP_MULTICAST_IF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
             if (o == null || !(o instanceof NetworkInterface))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                 throw new SocketException("bad argument for IP_MULTICAST_IF2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
             if (o == null || !(o instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                 throw new SocketException("bad argument for IP_MULTICAST_LOOP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
             break;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   348
         case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   349
             if (o == null || !(o instanceof Boolean)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   350
                 throw new SocketException("bad argument for SO_REUSEPORT");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   351
             }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   352
             if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   353
                 throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   354
             }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   355
             break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         socketSetOption(optID, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * get option's state - set or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public Object getOption(int optID) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Object result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        switch (optID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            case SO_TIMEOUT:
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   375
                result = timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if ( ((Integer)result).intValue() == -1) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   381
                    result = trafficClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            case IP_MULTICAST_IF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            case IP_MULTICAST_IF2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            case IP_MULTICAST_LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            case SO_BROADCAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                result = socketGetOption(optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   396
            case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   397
                if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   398
                    throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   399
                }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   400
                result = socketGetOption(optID);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   401
                break;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   402
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                throw new SocketException("invalid option: " + optID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    protected abstract void datagramSocketCreate() throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    protected abstract void datagramSocketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    protected abstract void socketSetOption(int opt, Object val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected abstract Object socketGetOption(int opt) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    protected abstract void connect0(InetAddress address, int port) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    protected abstract void disconnect0(int family);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   419
    protected boolean nativeConnectDisabled() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   420
        return connectDisabled;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 10693
diff changeset
   421
    }
27078
39275d6a8cac 8039509: Wrap sockets more thoroughly
michaelm
parents: 25859
diff changeset
   422
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 29986
diff changeset
   423
    abstract int dataAvailable();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   424
    private static native boolean isReusePortAvailable0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
}