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