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