src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
author michaelm
Fri, 22 Jun 2018 18:10:20 +0100
changeset 50722 bc104aaf24e9
parent 49249 92cca24c8807
child 54155 b5a73f22b2bd
child 57167 82874527373e
permissions -rw-r--r--
8204233: Add configurable option for enhanced socket IOException messages Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2018, 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: 2446
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: 2446
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: 2446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    28
import java.io.FileDescriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    32
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    33
import java.util.Collections;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    34
import java.util.HashSet;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
    35
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.ConnectionResetException;
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
    38
import sun.net.NetHooks;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
    39
import sun.net.ResourceManager;
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 49249
diff changeset
    40
import sun.net.util.SocketExceptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Default Socket Implementation. This implementation does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * not implement any security checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Note this class should <b>NOT</b> be public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Steven B. Byrne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
    49
abstract class AbstractPlainSocketImpl extends SocketImpl {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* instance variable for SO_TIMEOUT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int timeout;   // timeout in millisec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // traffic class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int trafficClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean shut_rd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean shut_wr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private SocketInputStream socketInputStream = null;
14173
8a94baea7747 7181793: Socket getOutputStream create streams that cannot be GC'ed until Socket is closed
coffeys
parents: 14014
diff changeset
    59
    private SocketOutputStream socketOutputStream = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* number of threads using the FileDescriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected int fdUseCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* lock when increment/decrementing fdUseCount */
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
    65
    protected final Object fdLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* indicates a close is pending on the file descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected boolean closePending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /* indicates connection reset state */
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
    71
    private volatile boolean connectionReset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
    73
   /* whether this Socket is a stream (TCP) socket or not (UDP)
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
    74
    */
10708
f255532bf336 7099488: TwoStacksPlainSocketImpl should invoke super.create(stream), typo in fix for 7098719
chegar
parents: 10704
diff changeset
    75
    protected boolean stream;
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Load net library into runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    82
            new java.security.PrivilegedAction<>() {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 10708
diff changeset
    83
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 10708
diff changeset
    84
                    System.loadLibrary("net");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 10708
diff changeset
    85
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 10708
diff changeset
    86
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 10708
diff changeset
    87
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    90
    private static volatile boolean checkedReusePort;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    91
    private static volatile boolean isReusePortAvailable;
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
    /**
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    94
     * Tells whether SO_REUSEPORT is supported.
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
    static boolean isReusePortAvailable() {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    97
        if (!checkedReusePort) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    98
            isReusePortAvailable = isReusePortAvailable0();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
    99
            checkedReusePort = true;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   100
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   101
        return isReusePortAvailable;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   102
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   103
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   104
    /**
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   105
     * Returns a set of SocketOptions supported by this impl and by this impl's
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   106
     * socket (Socket or ServerSocket)
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   107
     *
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   108
     * @return a Set of SocketOptions
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   109
     */
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   110
    @Override
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   111
    protected Set<SocketOption<?>> supportedOptions() {
38477
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   112
        Set<SocketOption<?>> options;
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   113
        if (isReusePortAvailable()) {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   114
            options = new HashSet<>();
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   115
            options.addAll(super.supportedOptions());
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   116
            options.add(StandardSocketOptions.SO_REUSEPORT);
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   117
            options = Collections.unmodifiableSet(options);
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   118
        } else {
f462865d453d 8143923: java.net socket supportedOptions set depends on call order
vtewari
parents: 36115
diff changeset
   119
            options = super.supportedOptions();
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   120
        }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   121
        return options;
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   122
    }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Creates a socket with a boolean that specifies whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * is a stream socket (true) or an unconnected UDP socket (false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected synchronized void create(boolean stream) throws IOException {
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   129
        this.stream = stream;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   130
        if (!stream) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   131
            ResourceManager.beforeUdpCreate();
10704
db5816a6e8c7 7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents: 10421
diff changeset
   132
            // only create the fd after we know we will be able to create the socket
db5816a6e8c7 7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents: 10421
diff changeset
   133
            fd = new FileDescriptor();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   134
            try {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   135
                socketCreate(false);
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
   136
                SocketCleanable.register(fd);
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   137
            } catch (IOException ioe) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   138
                ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   139
                fd = null;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   140
                throw ioe;
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   141
            }
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   142
        } else {
10704
db5816a6e8c7 7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents: 10421
diff changeset
   143
            fd = new FileDescriptor();
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   144
            socketCreate(true);
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
   145
            SocketCleanable.register(fd);
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   146
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (socket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            socket.setCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (serverSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            serverSocket.setCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Creates a socket and connects it to the specified port on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param host the specified host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param port the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void connect(String host, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        throws UnknownHostException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   162
        boolean connected = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            InetAddress address = InetAddress.getByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   168
            connectToAddress(address, port, timeout);
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   169
            connected = true;
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   170
        } finally {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   171
            if (!connected) {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   172
                try {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   173
                    close();
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   174
                } catch (IOException ioe) {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   175
                    /* Do nothing. If connect threw an exception then
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   176
                       it will be passed up the call stack */
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   177
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Creates a socket and connects it to the specified address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param port the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected void connect(InetAddress address, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            connectToAddress(address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // everything failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates a socket and connects it to the specified address on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the specified port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param address the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param timeout the timeout value in milliseconds, or zero for no timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws IOException if connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws  IllegalArgumentException if address is null or is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   212
    protected void connect(SocketAddress address, int timeout)
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   213
            throws IOException {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   214
        boolean connected = false;
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   215
        try {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   216
            if (address == null || !(address instanceof InetSocketAddress))
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   217
                throw new IllegalArgumentException("unsupported address type");
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   218
            InetSocketAddress addr = (InetSocketAddress) address;
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   219
            if (addr.isUnresolved())
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   220
                throw new UnknownHostException(addr.getHostName());
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   221
            this.port = addr.getPort();
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   222
            this.address = addr.getAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            connectToAddress(this.address, port, timeout);
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   225
            connected = true;
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   226
        } finally {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   227
            if (!connected) {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   228
                try {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   229
                    close();
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   230
                } catch (IOException ioe) {
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   231
                    /* Do nothing. If connect threw an exception then
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   232
                       it will be passed up the call stack */
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   233
                }
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   234
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private void connectToAddress(InetAddress address, int port, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (address.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            doConnect(InetAddress.getLocalHost(), port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            doConnect(address, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public void setOption(int opt, Object val) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean on = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            /* check type safety b4 going native.  These should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             * fail, since only java.Socket* has access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
             * PlainSocketImpl.setOption().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        case SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw new SocketException("Bad parameter for option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (val instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                /* true only if disabling - enabling should be Integer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                on = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        case SO_TIMEOUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (val == null || (!(val instanceof Integer)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new SocketException("Bad parameter for SO_TIMEOUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int tmp = ((Integer) val).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (tmp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                throw new IllegalArgumentException("timeout < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            timeout = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        case IP_TOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             if (val == null || !(val instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                 throw new SocketException("bad argument for IP_TOS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
             trafficClass = ((Integer)val).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new SocketException("Cannot re-bind socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        case TCP_NODELAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new SocketException("bad parameter for TCP_NODELAY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (val == null || !(val instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                !(((Integer)val).intValue() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                throw new SocketException("bad parameter for SO_SNDBUF " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                          "or SO_RCVBUF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        case SO_KEEPALIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                throw new SocketException("bad parameter for SO_KEEPALIVE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        case SO_OOBINLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new SocketException("bad parameter for SO_OOBINLINE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (val == null || !(val instanceof Boolean))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                throw new SocketException("bad parameter for SO_REUSEADDR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            on = ((Boolean)val).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            break;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   308
        case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   309
            if (val == null || !(val instanceof Boolean))
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   310
                throw new SocketException("bad parameter for SO_REUSEPORT");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   311
            if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT))
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   312
                throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   313
            on = ((Boolean)val).booleanValue();
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   314
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new SocketException("unrecognized TCP option: " + opt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        socketSetOption(opt, on, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public Object getOption(int opt) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new SocketException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (opt == SO_TIMEOUT) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   325
            return timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * The native socketGetOption() knows about 3 options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * The 32 bit value it returns will be interpreted according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * to what we're asking.  A return of -1 means it understands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * the option but its turned off.  It will raise a SocketException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * if "opt" isn't one it understands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        switch (opt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        case TCP_NODELAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        case SO_OOBINLINE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        case SO_LINGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ret = socketGetOption(opt, null);
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   345
            return (ret == -1) ? Boolean.FALSE: (Object)(ret);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        case SO_REUSEADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return Boolean.valueOf(ret != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        case SO_BINDADDR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            InetAddressContainer in = new InetAddressContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            ret = socketGetOption(opt, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return in.addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        case SO_SNDBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        case SO_RCVBUF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            ret = socketGetOption(opt, null);
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   356
            return ret;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        case IP_TOS:
30963
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   358
            try {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   359
                ret = socketGetOption(opt, null);
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   360
                if (ret == -1) { // ipv6 tos
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   361
                    return trafficClass;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   362
                } else {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   363
                    return ret;
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   364
                }
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   365
            } catch (SocketException se) {
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   366
                    // TODO - should make better effort to read TOS or TCLASS
88469d06e03f 8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents: 29986
diff changeset
   367
                    return trafficClass; // ipv6 tos
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        case SO_KEEPALIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            ret = socketGetOption(opt, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return Boolean.valueOf(ret != -1);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   372
        case SO_REUSEPORT:
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   373
            if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   374
                throw new UnsupportedOperationException("unsupported option");
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   375
            }
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   376
            ret = socketGetOption(opt, null);
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   377
            return Boolean.valueOf(ret != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // should never get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * The workhorse of the connection operation.  Tries several times to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * establish a connection to the given <host, port>.  If unsuccessful,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * throws an IOException indicating what went wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   391
        synchronized (fdLock) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   392
            if (!closePending && (socket == null || !socket.isBound())) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   393
                NetHooks.beforeTcpConnect(fd, address, port);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   394
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   395
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        try {
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   397
            acquireFD();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                socketConnect(address, port, timeout);
1335
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   400
                /* socket may have been closed during poll/select */
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   401
                synchronized (fdLock) {
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   402
                    if (closePending) {
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   403
                        throw new SocketException ("Socket closed");
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   404
                    }
79ed7fd4bb49 6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents: 715
diff changeset
   405
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // If we have a ref. to the Socket, then sets the flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // created, bound & connected to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // This is normally done in Socket.connect() but some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                // subclasses of Socket may call impl.connect() directly!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    socket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    socket.setConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            close();
50722
bc104aaf24e9 8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents: 49249
diff changeset
   419
            throw SocketExceptions.of(e, new InetSocketAddress(address, port));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Binds the socket to the specified address of the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param address the address
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12559
diff changeset
   426
     * @param lport the port
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    protected synchronized void bind(InetAddress address, int lport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    {
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   431
       synchronized (fdLock) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   432
            if (!closePending && (socket == null || !socket.isBound())) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   433
                NetHooks.beforeTcpBind(fd, address, lport);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   434
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents: 1335
diff changeset
   435
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        socketBind(address, lport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (socket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            socket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (serverSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            serverSocket.setBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Listens, for a specified amount of time, for connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param count the amount of time to listen for connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    protected synchronized void listen(int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        socketListen(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Accepts connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param s the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    protected void accept(SocketImpl s) throws IOException {
7027
d3404b1406d5 6994079: PlainSocketImpl should close the socket if it fails
chegar
parents: 5506
diff changeset
   456
        acquireFD();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            socketAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            releaseFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Gets an InputStream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    protected synchronized InputStream getInputStream() throws IOException {
14671
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   468
        synchronized (fdLock) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   469
            if (isClosedOrPending())
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   470
                throw new IOException("Socket Closed");
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   471
            if (shut_rd)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   472
                throw new IOException("Socket input is shutdown");
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   473
            if (socketInputStream == null)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   474
                socketInputStream = new SocketInputStream(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return socketInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    void setInputStream(SocketInputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        socketInputStream = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Gets an OutputStream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    protected synchronized OutputStream getOutputStream() throws IOException {
14671
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   487
        synchronized (fdLock) {
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   488
            if (isClosedOrPending())
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   489
                throw new IOException("Socket Closed");
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   490
            if (shut_wr)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   491
                throw new IOException("Socket output is shutdown");
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   492
            if (socketOutputStream == null)
22fad096737b 8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents: 14342
diff changeset
   493
                socketOutputStream = new SocketOutputStream(this);
14173
8a94baea7747 7181793: Socket getOutputStream create streams that cannot be GC'ed until Socket is closed
coffeys
parents: 14014
diff changeset
   494
        }
8a94baea7747 7181793: Socket getOutputStream create streams that cannot be GC'ed until Socket is closed
coffeys
parents: 14014
diff changeset
   495
        return socketOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    void setFileDescriptor(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    void setAddress(InetAddress address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    void setPort(int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    void setLocalPort(int localport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        this.localport = localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns the number of bytes that can be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    protected synchronized int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (isClosedOrPending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            throw new IOException("Stream closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        /*
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9035
diff changeset
   523
         * If connection has been reset or shut down for input, then return 0
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9035
diff changeset
   524
         * to indicate there are no buffered bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         */
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 9035
diff changeset
   526
        if (isConnectionReset() || shut_rd) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * If no bytes available and we were previously notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * of a connection reset then we move to the reset state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * If are notified of a connection reset then check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * again if there are bytes buffered on the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            n = socketAvailable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } catch (ConnectionResetException exc1) {
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
   541
            setConnectionReset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Closes the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    protected void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        synchronized(fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (fd != null) {
8817
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   552
                if (!stream) {
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   553
                    ResourceManager.afterUdpClose();
5351579c46a9 6981922: DNS cache poisoning by untrusted applets
michaelm
parents: 7668
diff changeset
   554
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                if (fdUseCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    if (closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                     * We close the FileDescriptor in two-steps - first the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                     * "pre-close" which closes the socket but doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                     * release the underlying file descriptor. This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                     * may be lengthy due to untransmitted data and a long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                     * linger interval. Once the pre-close is done we do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                     * actual socket to release the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        socketPreClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                     * If a thread has acquired the fd and a close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                     * isn't pending then use a deferred close.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                     * Also decrement fdUseCount to signal the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                     * thread that releases the fd to close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    if (!closePending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        closePending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        fdUseCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        socketPreClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Shutdown read-half of the socket connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    protected void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
      if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
          socketShutdown(SHUT_RD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
          if (socketInputStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
              socketInputStream.setEOF(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
          shut_rd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Shutdown write-half of the socket connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    protected void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
      if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
          socketShutdown(SHUT_WR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
          shut_wr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    protected boolean supportsUrgentData () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    protected void sendUrgentData (int data) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (fd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            throw new IOException("Socket Closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        socketSendUrgentData (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * "Acquires" and returns the FileDescriptor for this impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * A corresponding releaseFD is required to "release" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * FileDescriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    FileDescriptor acquireFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            fdUseCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * "Release" the FileDescriptor for this impl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * If the use count goes to -1 then the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    void releaseFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            fdUseCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if (fdUseCount == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                if (fd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        socketClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        fd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
   669
    boolean isConnectionReset() {
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
   670
        return connectionReset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
49249
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
   673
    void setConnectionReset() {
92cca24c8807 8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents: 48737
diff changeset
   674
        connectionReset = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Return true if already closed or close is pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public boolean isClosedOrPending() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * Lock on fdLock to ensure that we wait if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         * close is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        synchronized (fdLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (closePending || (fd == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Return the current value of SO_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public int getTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * "Pre-close" a socket by dup'ing the file descriptor - this enables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * the socket to be closed without releasing the file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    private void socketPreClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        socketClose0(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Close the socket (and release the file descriptor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    protected void socketClose() throws IOException {
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents: 47216
diff changeset
   713
        SocketCleanable.unregister(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        socketClose0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    abstract void socketCreate(boolean isServer) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    abstract void socketConnect(InetAddress address, int port, int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    abstract void socketBind(InetAddress address, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    abstract void socketListen(int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    abstract void socketAccept(SocketImpl s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    abstract int socketAvailable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    abstract void socketClose0(boolean useDeferredClose)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    abstract void socketShutdown(int howto)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    abstract void socketSetOption(int cmd, boolean on, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    abstract int socketGetOption(int opt, Object iaContainerObj) throws SocketException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    abstract void socketSendUrgentData(int data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30963
diff changeset
   738
    public static final int SHUT_RD = 0;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30963
diff changeset
   739
    public static final int SHUT_WR = 1;
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   740
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 32649
diff changeset
   741
    private static native boolean isReusePortAvailable0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
}