author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 55693 | 9a97b1393e72 |
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; |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
38 |
import java.util.Objects; |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
39 |
import java.util.Set; |
2 | 40 |
|
41 |
import sun.net.ConnectionResetException; |
|
2446 | 42 |
import sun.net.NetHooks; |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
43 |
import sun.net.PlatformSocketImpl; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
44 |
import sun.net.ResourceManager; |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
45 |
import sun.net.ext.ExtendedSocketOptions; |
55375 | 46 |
import sun.net.util.IPAddressUtil; |
50722
bc104aaf24e9
8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents:
49249
diff
changeset
|
47 |
import sun.net.util.SocketExceptions; |
2 | 48 |
|
49 |
/** |
|
50 |
* Default Socket Implementation. This implementation does |
|
51 |
* not implement any security checks. |
|
52 |
* Note this class should <b>NOT</b> be public. |
|
53 |
* |
|
54 |
* @author Steven B. Byrne |
|
55 |
*/ |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
56 |
abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl { |
2 | 57 |
/* instance variable for SO_TIMEOUT */ |
58 |
int timeout; // timeout in millisec |
|
59 |
// traffic class |
|
60 |
private int trafficClass; |
|
61 |
||
62 |
private boolean shut_rd = false; |
|
63 |
private boolean shut_wr = false; |
|
64 |
||
65 |
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
|
66 |
private SocketOutputStream socketOutputStream = null; |
2 | 67 |
|
68 |
/* number of threads using the FileDescriptor */ |
|
69 |
protected int fdUseCount = 0; |
|
70 |
||
71 |
/* lock when increment/decrementing fdUseCount */ |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
72 |
protected final Object fdLock = new Object(); |
2 | 73 |
|
74 |
/* indicates a close is pending on the file descriptor */ |
|
75 |
protected boolean closePending = false; |
|
76 |
||
77 |
/* indicates connection reset state */ |
|
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
78 |
private volatile boolean connectionReset; |
2 | 79 |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
80 |
/* indicates whether impl is bound */ |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
81 |
boolean isBound; |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
82 |
|
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
83 |
/* indicates whether impl is connected */ |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
84 |
volatile boolean isConnected; |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
85 |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
86 |
/* whether this Socket is a stream (TCP) socket or not (UDP) |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
87 |
*/ |
10708
f255532bf336
7099488: TwoStacksPlainSocketImpl should invoke super.create(stream), typo in fix for 7098719
chegar
parents:
10704
diff
changeset
|
88 |
protected boolean stream; |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
89 |
|
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
90 |
/* whether this is a server or not */ |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
91 |
final boolean isServer; |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
92 |
|
2 | 93 |
/** |
94 |
* Load net library into runtime. |
|
95 |
*/ |
|
96 |
static { |
|
55693
9a97b1393e72
8227587: Add internal privileged System.loadLibrary
redestad
parents:
55375
diff
changeset
|
97 |
jdk.internal.loader.BootLoader.loadLibrary("net"); |
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) { |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
115 |
this.isServer = isServer; |
36115 | 116 |
} |
117 |
||
2 | 118 |
/** |
119 |
* Creates a socket with a boolean that specifies whether this |
|
120 |
* is a stream socket (true) or an unconnected UDP socket (false). |
|
121 |
*/ |
|
122 |
protected synchronized void create(boolean stream) throws IOException { |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
123 |
this.stream = stream; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
124 |
if (!stream) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
125 |
ResourceManager.beforeUdpCreate(); |
10704
db5816a6e8c7
7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents:
10421
diff
changeset
|
126 |
// 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
|
127 |
fd = new FileDescriptor(); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
128 |
try { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
129 |
socketCreate(false); |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
130 |
SocketCleanable.register(fd); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
131 |
} catch (IOException ioe) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
132 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
133 |
fd = null; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
134 |
throw ioe; |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
135 |
} |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
136 |
} else { |
10704
db5816a6e8c7
7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents:
10421
diff
changeset
|
137 |
fd = new FileDescriptor(); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
138 |
socketCreate(true); |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
139 |
SocketCleanable.register(fd); |
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
140 |
} |
2 | 141 |
} |
142 |
||
143 |
/** |
|
144 |
* Creates a socket and connects it to the specified port on |
|
145 |
* the specified host. |
|
146 |
* @param host the specified host |
|
147 |
* @param port the specified port |
|
148 |
*/ |
|
149 |
protected void connect(String host, int port) |
|
150 |
throws UnknownHostException, IOException |
|
151 |
{ |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
152 |
boolean connected = false; |
2 | 153 |
try { |
154 |
InetAddress address = InetAddress.getByName(host); |
|
55375 | 155 |
// recording this.address as supplied by caller before calling connect |
156 |
this.address = address; |
|
2 | 157 |
this.port = port; |
55375 | 158 |
if (address.isLinkLocalAddress()) { |
159 |
address = IPAddressUtil.toScopedAddress(address); |
|
160 |
} |
|
2 | 161 |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
162 |
connectToAddress(address, port, timeout); |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
163 |
connected = true; |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
164 |
} finally { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
165 |
if (!connected) { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
166 |
try { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
167 |
close(); |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
168 |
} catch (IOException ioe) { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
169 |
/* Do nothing. If connect threw an exception then |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
170 |
it will be passed up the call stack */ |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
171 |
} |
2 | 172 |
} |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
173 |
isConnected = connected; |
2 | 174 |
} |
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Creates a socket and connects it to the specified address on |
|
179 |
* the specified port. |
|
180 |
* @param address the address |
|
181 |
* @param port the specified port |
|
182 |
*/ |
|
183 |
protected void connect(InetAddress address, int port) throws IOException { |
|
55375 | 184 |
// recording this.address as supplied by caller before calling connect |
185 |
this.address = address; |
|
2 | 186 |
this.port = port; |
55375 | 187 |
if (address.isLinkLocalAddress()) { |
188 |
address = IPAddressUtil.toScopedAddress(address); |
|
189 |
} |
|
2 | 190 |
|
191 |
try { |
|
192 |
connectToAddress(address, port, timeout); |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
193 |
isConnected = true; |
2 | 194 |
return; |
195 |
} catch (IOException e) { |
|
196 |
// everything failed |
|
197 |
close(); |
|
198 |
throw e; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Creates a socket and connects it to the specified address on |
|
204 |
* the specified port. |
|
205 |
* @param address the address |
|
206 |
* @param timeout the timeout value in milliseconds, or zero for no timeout. |
|
207 |
* @throws IOException if connection fails |
|
208 |
* @throws IllegalArgumentException if address is null or is a |
|
209 |
* SocketAddress subclass not supported by this socket |
|
210 |
* @since 1.4 |
|
211 |
*/ |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
212 |
protected void connect(SocketAddress address, int timeout) |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
213 |
throws IOException { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
214 |
boolean connected = false; |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
215 |
try { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
216 |
if (address == null || !(address instanceof InetSocketAddress)) |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
217 |
throw new IllegalArgumentException("unsupported address type"); |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
218 |
InetSocketAddress addr = (InetSocketAddress) address; |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
219 |
if (addr.isUnresolved()) |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
220 |
throw new UnknownHostException(addr.getHostName()); |
55375 | 221 |
// recording this.address as supplied by caller before calling connect |
222 |
InetAddress ia = addr.getAddress(); |
|
223 |
this.address = ia; |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
224 |
this.port = addr.getPort(); |
55375 | 225 |
if (ia.isLinkLocalAddress()) { |
226 |
ia = IPAddressUtil.toScopedAddress(ia); |
|
227 |
} |
|
228 |
connectToAddress(ia, port, timeout); |
|
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
229 |
connected = true; |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
230 |
} finally { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
231 |
if (!connected) { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
232 |
try { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
233 |
close(); |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
234 |
} catch (IOException ioe) { |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
235 |
/* Do nothing. If connect threw an exception then |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
236 |
it will be passed up the call stack */ |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
237 |
} |
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
238 |
} |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
239 |
isConnected = connected; |
2 | 240 |
} |
241 |
} |
|
242 |
||
243 |
private void connectToAddress(InetAddress address, int port, int timeout) throws IOException { |
|
244 |
if (address.isAnyLocalAddress()) { |
|
245 |
doConnect(InetAddress.getLocalHost(), port, timeout); |
|
246 |
} else { |
|
247 |
doConnect(address, port, timeout); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
public void setOption(int opt, Object val) throws SocketException { |
|
252 |
if (isClosedOrPending()) { |
|
253 |
throw new SocketException("Socket Closed"); |
|
254 |
} |
|
255 |
boolean on = true; |
|
256 |
switch (opt) { |
|
257 |
/* check type safety b4 going native. These should never |
|
258 |
* fail, since only java.Socket* has access to |
|
259 |
* PlainSocketImpl.setOption(). |
|
260 |
*/ |
|
261 |
case SO_LINGER: |
|
262 |
if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean))) |
|
263 |
throw new SocketException("Bad parameter for option"); |
|
264 |
if (val instanceof Boolean) { |
|
265 |
/* true only if disabling - enabling should be Integer */ |
|
266 |
on = false; |
|
267 |
} |
|
268 |
break; |
|
269 |
case SO_TIMEOUT: |
|
270 |
if (val == null || (!(val instanceof Integer))) |
|
271 |
throw new SocketException("Bad parameter for SO_TIMEOUT"); |
|
272 |
int tmp = ((Integer) val).intValue(); |
|
273 |
if (tmp < 0) |
|
274 |
throw new IllegalArgumentException("timeout < 0"); |
|
275 |
timeout = tmp; |
|
276 |
break; |
|
277 |
case IP_TOS: |
|
278 |
if (val == null || !(val instanceof Integer)) { |
|
279 |
throw new SocketException("bad argument for IP_TOS"); |
|
280 |
} |
|
281 |
trafficClass = ((Integer)val).intValue(); |
|
282 |
break; |
|
283 |
case SO_BINDADDR: |
|
284 |
throw new SocketException("Cannot re-bind socket"); |
|
285 |
case TCP_NODELAY: |
|
286 |
if (val == null || !(val instanceof Boolean)) |
|
287 |
throw new SocketException("bad parameter for TCP_NODELAY"); |
|
288 |
on = ((Boolean)val).booleanValue(); |
|
289 |
break; |
|
290 |
case SO_SNDBUF: |
|
291 |
case SO_RCVBUF: |
|
292 |
if (val == null || !(val instanceof Integer) || |
|
293 |
!(((Integer)val).intValue() > 0)) { |
|
294 |
throw new SocketException("bad parameter for SO_SNDBUF " + |
|
295 |
"or SO_RCVBUF"); |
|
296 |
} |
|
297 |
break; |
|
298 |
case SO_KEEPALIVE: |
|
299 |
if (val == null || !(val instanceof Boolean)) |
|
300 |
throw new SocketException("bad parameter for SO_KEEPALIVE"); |
|
301 |
on = ((Boolean)val).booleanValue(); |
|
302 |
break; |
|
303 |
case SO_OOBINLINE: |
|
304 |
if (val == null || !(val instanceof Boolean)) |
|
305 |
throw new SocketException("bad parameter for SO_OOBINLINE"); |
|
306 |
on = ((Boolean)val).booleanValue(); |
|
307 |
break; |
|
308 |
case SO_REUSEADDR: |
|
309 |
if (val == null || !(val instanceof Boolean)) |
|
310 |
throw new SocketException("bad parameter for SO_REUSEADDR"); |
|
311 |
on = ((Boolean)val).booleanValue(); |
|
312 |
break; |
|
36115 | 313 |
case SO_REUSEPORT: |
314 |
if (val == null || !(val instanceof Boolean)) |
|
315 |
throw new SocketException("bad parameter for SO_REUSEPORT"); |
|
316 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) |
|
317 |
throw new UnsupportedOperationException("unsupported option"); |
|
318 |
on = ((Boolean)val).booleanValue(); |
|
319 |
break; |
|
2 | 320 |
default: |
321 |
throw new SocketException("unrecognized TCP option: " + opt); |
|
322 |
} |
|
323 |
socketSetOption(opt, on, val); |
|
324 |
} |
|
325 |
public Object getOption(int opt) throws SocketException { |
|
326 |
if (isClosedOrPending()) { |
|
327 |
throw new SocketException("Socket Closed"); |
|
328 |
} |
|
329 |
if (opt == SO_TIMEOUT) { |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
330 |
return timeout; |
2 | 331 |
} |
332 |
int ret = 0; |
|
333 |
/* |
|
334 |
* The native socketGetOption() knows about 3 options. |
|
335 |
* The 32 bit value it returns will be interpreted according |
|
336 |
* to what we're asking. A return of -1 means it understands |
|
337 |
* the option but its turned off. It will raise a SocketException |
|
338 |
* if "opt" isn't one it understands. |
|
339 |
*/ |
|
340 |
||
341 |
switch (opt) { |
|
342 |
case TCP_NODELAY: |
|
343 |
ret = socketGetOption(opt, null); |
|
344 |
return Boolean.valueOf(ret != -1); |
|
345 |
case SO_OOBINLINE: |
|
346 |
ret = socketGetOption(opt, null); |
|
347 |
return Boolean.valueOf(ret != -1); |
|
348 |
case SO_LINGER: |
|
349 |
ret = socketGetOption(opt, null); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
350 |
return (ret == -1) ? Boolean.FALSE: (Object)(ret); |
2 | 351 |
case SO_REUSEADDR: |
352 |
ret = socketGetOption(opt, null); |
|
353 |
return Boolean.valueOf(ret != -1); |
|
354 |
case SO_BINDADDR: |
|
355 |
InetAddressContainer in = new InetAddressContainer(); |
|
356 |
ret = socketGetOption(opt, in); |
|
357 |
return in.addr; |
|
358 |
case SO_SNDBUF: |
|
359 |
case SO_RCVBUF: |
|
360 |
ret = socketGetOption(opt, null); |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
23010
diff
changeset
|
361 |
return ret; |
2 | 362 |
case IP_TOS: |
30963
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
363 |
try { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
364 |
ret = socketGetOption(opt, null); |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
365 |
if (ret == -1) { // ipv6 tos |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
366 |
return trafficClass; |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
367 |
} else { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
368 |
return ret; |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
369 |
} |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
370 |
} catch (SocketException se) { |
88469d06e03f
8072384: Setting IP_TOS on java.net sockets not working on unix
coffeys
parents:
29986
diff
changeset
|
371 |
// 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
|
372 |
return trafficClass; // ipv6 tos |
2 | 373 |
} |
374 |
case SO_KEEPALIVE: |
|
375 |
ret = socketGetOption(opt, null); |
|
376 |
return Boolean.valueOf(ret != -1); |
|
36115 | 377 |
case SO_REUSEPORT: |
378 |
if (!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) { |
|
379 |
throw new UnsupportedOperationException("unsupported option"); |
|
380 |
} |
|
381 |
ret = socketGetOption(opt, null); |
|
382 |
return Boolean.valueOf(ret != -1); |
|
2 | 383 |
// should never get here |
384 |
default: |
|
385 |
return null; |
|
386 |
} |
|
387 |
} |
|
388 |
||
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
389 |
static final ExtendedSocketOptions extendedOptions = |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
390 |
ExtendedSocketOptions.getInstance(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
391 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
392 |
private static final Set<SocketOption<?>> clientSocketOptions = clientSocketOptions(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
393 |
private static final Set<SocketOption<?>> serverSocketOptions = serverSocketOptions(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
394 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
395 |
private static Set<SocketOption<?>> clientSocketOptions() { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
396 |
HashSet<SocketOption<?>> options = new HashSet<>(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
397 |
options.add(StandardSocketOptions.SO_KEEPALIVE); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
398 |
options.add(StandardSocketOptions.SO_SNDBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
399 |
options.add(StandardSocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
400 |
options.add(StandardSocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
401 |
options.add(StandardSocketOptions.SO_LINGER); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
402 |
options.add(StandardSocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
403 |
options.add(StandardSocketOptions.TCP_NODELAY); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
404 |
if (isReusePortAvailable()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
405 |
options.add(StandardSocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
406 |
options.addAll(ExtendedSocketOptions.clientSocketOptions()); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
407 |
return Collections.unmodifiableSet(options); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
408 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
409 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
410 |
private static Set<SocketOption<?>> serverSocketOptions() { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
411 |
HashSet<SocketOption<?>> options = new HashSet<>(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
412 |
options.add(StandardSocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
413 |
options.add(StandardSocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
414 |
options.add(StandardSocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
415 |
if (isReusePortAvailable()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
416 |
options.add(StandardSocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
417 |
options.addAll(ExtendedSocketOptions.serverSocketOptions()); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
418 |
return Collections.unmodifiableSet(options); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
419 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
420 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
421 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
422 |
protected Set<SocketOption<?>> supportedOptions() { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
423 |
if (isServer) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
424 |
return serverSocketOptions; |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
425 |
else |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
426 |
return clientSocketOptions; |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
427 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
428 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
429 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
430 |
protected <T> void setOption(SocketOption<T> name, T value) throws IOException { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
431 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
432 |
if (!supportedOptions().contains(name)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
433 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
434 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
435 |
if (!name.type().isInstance(value)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
436 |
throw new IllegalArgumentException("Invalid value '" + value + "'"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
437 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
438 |
if (isClosedOrPending()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
439 |
throw new SocketException("Socket closed"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
440 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
441 |
if (name == StandardSocketOptions.SO_KEEPALIVE) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
442 |
setOption(SocketOptions.SO_KEEPALIVE, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
443 |
} else if (name == StandardSocketOptions.SO_SNDBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
444 |
if (((Integer)value).intValue() < 0) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
445 |
throw new IllegalArgumentException("Invalid send buffer size:" + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
446 |
setOption(SocketOptions.SO_SNDBUF, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
447 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
448 |
if (((Integer)value).intValue() < 0) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
449 |
throw new IllegalArgumentException("Invalid recv buffer size:" + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
450 |
setOption(SocketOptions.SO_RCVBUF, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
451 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
452 |
setOption(SocketOptions.SO_REUSEADDR, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
453 |
} else if (name == StandardSocketOptions.SO_REUSEPORT) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
454 |
setOption(SocketOptions.SO_REUSEPORT, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
455 |
} else if (name == StandardSocketOptions.SO_LINGER ) { |
55226
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
456 |
if (((Integer)value).intValue() < 0) |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
457 |
setOption(SocketOptions.SO_LINGER, false); |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
458 |
else |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
459 |
setOption(SocketOptions.SO_LINGER, value); |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
460 |
} else if (name == StandardSocketOptions.IP_TOS) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
461 |
int i = ((Integer)value).intValue(); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
462 |
if (i < 0 || i > 255) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
463 |
throw new IllegalArgumentException("Invalid IP_TOS value: " + value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
464 |
setOption(SocketOptions.IP_TOS, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
465 |
} else if (name == StandardSocketOptions.TCP_NODELAY) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
466 |
setOption(SocketOptions.TCP_NODELAY, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
467 |
} else if (extendedOptions.isOptionSupported(name)) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
468 |
extendedOptions.setOption(fd, name, value); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
469 |
} else { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
470 |
throw new AssertionError("unknown option: " + name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
471 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
472 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
473 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
474 |
@Override |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
475 |
@SuppressWarnings("unchecked") |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
476 |
protected <T> T getOption(SocketOption<T> name) throws IOException { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
477 |
Objects.requireNonNull(name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
478 |
if (!supportedOptions().contains(name)) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
479 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
480 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
481 |
if (isClosedOrPending()) |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
482 |
throw new SocketException("Socket closed"); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
483 |
|
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
484 |
if (name == StandardSocketOptions.SO_KEEPALIVE) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
485 |
return (T)getOption(SocketOptions.SO_KEEPALIVE); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
486 |
} else if (name == StandardSocketOptions.SO_SNDBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
487 |
return (T)getOption(SocketOptions.SO_SNDBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
488 |
} else if (name == StandardSocketOptions.SO_RCVBUF) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
489 |
return (T)getOption(SocketOptions.SO_RCVBUF); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
490 |
} else if (name == StandardSocketOptions.SO_REUSEADDR) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
491 |
return (T)getOption(SocketOptions.SO_REUSEADDR); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
492 |
} else if (name == StandardSocketOptions.SO_REUSEPORT) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
493 |
return (T)getOption(SocketOptions.SO_REUSEPORT); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
494 |
} else if (name == StandardSocketOptions.SO_LINGER) { |
55226
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
495 |
Object value = getOption(SocketOptions.SO_LINGER); |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
496 |
if (value instanceof Boolean) { |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
497 |
assert ((Boolean)value).booleanValue() == false; |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
498 |
value = -1; |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
499 |
} |
ce8bab2c4185
8225214: Socket.getOption(SocketOption) not returning the expected type for the StandardSocketOptions.SO_LINGER
chegar
parents:
55081
diff
changeset
|
500 |
return (T)value; |
55081
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
501 |
} else if (name == StandardSocketOptions.IP_TOS) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
502 |
return (T)getOption(SocketOptions.IP_TOS); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
503 |
} else if (name == StandardSocketOptions.TCP_NODELAY) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
504 |
return (T)getOption(SocketOptions.TCP_NODELAY); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
505 |
} else if (extendedOptions.isOptionSupported(name)) { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
506 |
return (T) extendedOptions.getOption(fd, name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
507 |
} else { |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
508 |
throw new AssertionError("unknown option: " + name); |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
509 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
510 |
} |
dd321e3596c0
8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents:
54689
diff
changeset
|
511 |
|
2 | 512 |
/** |
513 |
* The workhorse of the connection operation. Tries several times to |
|
514 |
* establish a connection to the given <host, port>. If unsuccessful, |
|
515 |
* throws an IOException indicating what went wrong. |
|
516 |
*/ |
|
517 |
||
518 |
synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException { |
|
2446 | 519 |
synchronized (fdLock) { |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
520 |
if (!closePending && !isBound) { |
2446 | 521 |
NetHooks.beforeTcpConnect(fd, address, port); |
522 |
} |
|
523 |
} |
|
2 | 524 |
try { |
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
525 |
acquireFD(); |
2 | 526 |
try { |
527 |
socketConnect(address, port, timeout); |
|
1335
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
528 |
/* 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
|
529 |
synchronized (fdLock) { |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
530 |
if (closePending) { |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
531 |
throw new SocketException ("Socket closed"); |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
532 |
} |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
533 |
} |
2 | 534 |
} finally { |
535 |
releaseFD(); |
|
536 |
} |
|
537 |
} catch (IOException e) { |
|
538 |
close(); |
|
50722
bc104aaf24e9
8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents:
49249
diff
changeset
|
539 |
throw SocketExceptions.of(e, new InetSocketAddress(address, port)); |
2 | 540 |
} |
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Binds the socket to the specified address of the specified local port. |
|
545 |
* @param address the address |
|
14014 | 546 |
* @param lport the port |
2 | 547 |
*/ |
548 |
protected synchronized void bind(InetAddress address, int lport) |
|
549 |
throws IOException |
|
550 |
{ |
|
2446 | 551 |
synchronized (fdLock) { |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
552 |
if (!closePending && !isBound) { |
2446 | 553 |
NetHooks.beforeTcpBind(fd, address, lport); |
554 |
} |
|
555 |
} |
|
55375 | 556 |
if (address.isLinkLocalAddress()) { |
557 |
address = IPAddressUtil.toScopedAddress(address); |
|
558 |
} |
|
2 | 559 |
socketBind(address, lport); |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
560 |
isBound = true; |
2 | 561 |
} |
562 |
||
563 |
/** |
|
564 |
* Listens, for a specified amount of time, for connections. |
|
565 |
* @param count the amount of time to listen for connections |
|
566 |
*/ |
|
567 |
protected synchronized void listen(int count) throws IOException { |
|
568 |
socketListen(count); |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Accepts connections. |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
573 |
* @param si the socket impl |
2 | 574 |
*/ |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
575 |
protected void accept(SocketImpl si) throws IOException { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
576 |
si.fd = new FileDescriptor(); |
7027
d3404b1406d5
6994079: PlainSocketImpl should close the socket if it fails
chegar
parents:
5506
diff
changeset
|
577 |
acquireFD(); |
2 | 578 |
try { |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
579 |
socketAccept(si); |
2 | 580 |
} finally { |
581 |
releaseFD(); |
|
582 |
} |
|
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
583 |
SocketCleanable.register(si.fd); |
2 | 584 |
} |
585 |
||
586 |
/** |
|
587 |
* Gets an InputStream for this socket. |
|
588 |
*/ |
|
589 |
protected synchronized InputStream getInputStream() throws IOException { |
|
14671
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
590 |
synchronized (fdLock) { |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
591 |
if (isClosedOrPending()) |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
592 |
throw new IOException("Socket Closed"); |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
593 |
if (shut_rd) |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
594 |
throw new IOException("Socket input is shutdown"); |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
595 |
if (socketInputStream == null) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
596 |
PrivilegedExceptionAction<SocketInputStream> pa = () -> new SocketInputStream(this); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
597 |
try { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
598 |
socketInputStream = AccessController.doPrivileged(pa); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
599 |
} catch (PrivilegedActionException e) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
600 |
throw (IOException) e.getCause(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
601 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
602 |
} |
2 | 603 |
} |
604 |
return socketInputStream; |
|
605 |
} |
|
606 |
||
607 |
void setInputStream(SocketInputStream in) { |
|
608 |
socketInputStream = in; |
|
609 |
} |
|
610 |
||
611 |
/** |
|
612 |
* Gets an OutputStream for this socket. |
|
613 |
*/ |
|
614 |
protected synchronized OutputStream getOutputStream() throws IOException { |
|
14671
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
615 |
synchronized (fdLock) { |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
616 |
if (isClosedOrPending()) |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
617 |
throw new IOException("Socket Closed"); |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
618 |
if (shut_wr) |
22fad096737b
8003833: Spurious NPE from Socket.getIn/OutputStream
chegar
parents:
14342
diff
changeset
|
619 |
throw new IOException("Socket output is shutdown"); |
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
620 |
if (socketOutputStream == null) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
621 |
PrivilegedExceptionAction<SocketOutputStream> pa = () -> new SocketOutputStream(this); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
622 |
try { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
623 |
socketOutputStream = AccessController.doPrivileged(pa); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
624 |
} catch (PrivilegedActionException e) { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
625 |
throw (IOException) e.getCause(); |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
626 |
} |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
627 |
} |
14173
8a94baea7747
7181793: Socket getOutputStream create streams that cannot be GC'ed until Socket is closed
coffeys
parents:
14014
diff
changeset
|
628 |
} |
8a94baea7747
7181793: Socket getOutputStream create streams that cannot be GC'ed until Socket is closed
coffeys
parents:
14014
diff
changeset
|
629 |
return socketOutputStream; |
2 | 630 |
} |
631 |
||
632 |
void setFileDescriptor(FileDescriptor fd) { |
|
633 |
this.fd = fd; |
|
634 |
} |
|
635 |
||
636 |
void setAddress(InetAddress address) { |
|
637 |
this.address = address; |
|
638 |
} |
|
639 |
||
640 |
void setPort(int port) { |
|
641 |
this.port = port; |
|
642 |
} |
|
643 |
||
644 |
void setLocalPort(int localport) { |
|
645 |
this.localport = localport; |
|
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Returns the number of bytes that can be read without blocking. |
|
650 |
*/ |
|
651 |
protected synchronized int available() throws IOException { |
|
652 |
if (isClosedOrPending()) { |
|
653 |
throw new IOException("Stream closed."); |
|
654 |
} |
|
655 |
||
656 |
/* |
|
10421
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
9035
diff
changeset
|
657 |
* 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
|
658 |
* to indicate there are no buffered bytes. |
2 | 659 |
*/ |
10421
2ee16a0f6df5
7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
9035
diff
changeset
|
660 |
if (isConnectionReset() || shut_rd) { |
2 | 661 |
return 0; |
662 |
} |
|
663 |
||
664 |
/* |
|
665 |
* If no bytes available and we were previously notified |
|
666 |
* of a connection reset then we move to the reset state. |
|
667 |
* |
|
668 |
* If are notified of a connection reset then check |
|
669 |
* again if there are bytes buffered on the socket. |
|
670 |
*/ |
|
671 |
int n = 0; |
|
672 |
try { |
|
673 |
n = socketAvailable(); |
|
674 |
} catch (ConnectionResetException exc1) { |
|
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
675 |
setConnectionReset(); |
2 | 676 |
} |
677 |
return n; |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Closes the socket. |
|
682 |
*/ |
|
683 |
protected void close() throws IOException { |
|
684 |
synchronized(fdLock) { |
|
685 |
if (fd != null) { |
|
8817
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
686 |
if (!stream) { |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
687 |
ResourceManager.afterUdpClose(); |
5351579c46a9
6981922: DNS cache poisoning by untrusted applets
michaelm
parents:
7668
diff
changeset
|
688 |
} |
2 | 689 |
if (fdUseCount == 0) { |
690 |
if (closePending) { |
|
691 |
return; |
|
692 |
} |
|
693 |
closePending = true; |
|
694 |
/* |
|
695 |
* We close the FileDescriptor in two-steps - first the |
|
696 |
* "pre-close" which closes the socket but doesn't |
|
697 |
* release the underlying file descriptor. This operation |
|
698 |
* may be lengthy due to untransmitted data and a long |
|
699 |
* linger interval. Once the pre-close is done we do the |
|
700 |
* actual socket to release the fd. |
|
701 |
*/ |
|
702 |
try { |
|
703 |
socketPreClose(); |
|
704 |
} finally { |
|
705 |
socketClose(); |
|
706 |
} |
|
707 |
fd = null; |
|
708 |
return; |
|
709 |
} else { |
|
710 |
/* |
|
711 |
* If a thread has acquired the fd and a close |
|
712 |
* isn't pending then use a deferred close. |
|
713 |
* Also decrement fdUseCount to signal the last |
|
714 |
* thread that releases the fd to close it. |
|
715 |
*/ |
|
716 |
if (!closePending) { |
|
717 |
closePending = true; |
|
718 |
fdUseCount--; |
|
719 |
socketPreClose(); |
|
720 |
} |
|
721 |
} |
|
722 |
} |
|
723 |
} |
|
724 |
} |
|
725 |
||
54155
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
726 |
void reset() { |
b5a73f22b2bd
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents:
50722
diff
changeset
|
727 |
throw new InternalError("should not get here"); |
2 | 728 |
} |
729 |
||
730 |
/** |
|
731 |
* Shutdown read-half of the socket connection; |
|
732 |
*/ |
|
733 |
protected void shutdownInput() throws IOException { |
|
734 |
if (fd != null) { |
|
735 |
socketShutdown(SHUT_RD); |
|
736 |
if (socketInputStream != null) { |
|
737 |
socketInputStream.setEOF(true); |
|
738 |
} |
|
739 |
shut_rd = true; |
|
740 |
} |
|
741 |
} |
|
742 |
||
743 |
/** |
|
744 |
* Shutdown write-half of the socket connection; |
|
745 |
*/ |
|
746 |
protected void shutdownOutput() throws IOException { |
|
747 |
if (fd != null) { |
|
748 |
socketShutdown(SHUT_WR); |
|
749 |
shut_wr = true; |
|
750 |
} |
|
751 |
} |
|
752 |
||
753 |
protected boolean supportsUrgentData () { |
|
754 |
return true; |
|
755 |
} |
|
756 |
||
757 |
protected void sendUrgentData (int data) throws IOException { |
|
758 |
if (fd == null) { |
|
759 |
throw new IOException("Socket Closed"); |
|
760 |
} |
|
761 |
socketSendUrgentData (data); |
|
762 |
} |
|
763 |
||
764 |
/* |
|
765 |
* "Acquires" and returns the FileDescriptor for this impl |
|
766 |
* |
|
767 |
* A corresponding releaseFD is required to "release" the |
|
768 |
* FileDescriptor. |
|
769 |
*/ |
|
770 |
FileDescriptor acquireFD() { |
|
771 |
synchronized (fdLock) { |
|
772 |
fdUseCount++; |
|
773 |
return fd; |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
/* |
|
778 |
* "Release" the FileDescriptor for this impl. |
|
779 |
* |
|
780 |
* If the use count goes to -1 then the socket is closed. |
|
781 |
*/ |
|
782 |
void releaseFD() { |
|
783 |
synchronized (fdLock) { |
|
784 |
fdUseCount--; |
|
785 |
if (fdUseCount == -1) { |
|
786 |
if (fd != null) { |
|
787 |
try { |
|
788 |
socketClose(); |
|
789 |
} catch (IOException e) { |
|
790 |
} finally { |
|
791 |
fd = null; |
|
792 |
} |
|
793 |
} |
|
794 |
} |
|
795 |
} |
|
796 |
} |
|
797 |
||
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
798 |
boolean isConnectionReset() { |
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
799 |
return connectionReset; |
2 | 800 |
} |
801 |
||
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
802 |
void setConnectionReset() { |
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48737
diff
changeset
|
803 |
connectionReset = true; |
2 | 804 |
} |
805 |
||
806 |
/* |
|
807 |
* Return true if already closed or close is pending |
|
808 |
*/ |
|
809 |
public boolean isClosedOrPending() { |
|
810 |
/* |
|
811 |
* Lock on fdLock to ensure that we wait if a |
|
812 |
* close is in progress. |
|
813 |
*/ |
|
814 |
synchronized (fdLock) { |
|
815 |
if (closePending || (fd == null)) { |
|
816 |
return true; |
|
817 |
} else { |
|
818 |
return false; |
|
819 |
} |
|
820 |
} |
|
821 |
} |
|
822 |
||
823 |
/* |
|
824 |
* Return the current value of SO_TIMEOUT |
|
825 |
*/ |
|
826 |
public int getTimeout() { |
|
827 |
return timeout; |
|
828 |
} |
|
829 |
||
830 |
/* |
|
831 |
* "Pre-close" a socket by dup'ing the file descriptor - this enables |
|
832 |
* the socket to be closed without releasing the file descriptor. |
|
833 |
*/ |
|
834 |
private void socketPreClose() throws IOException { |
|
835 |
socketClose0(true); |
|
836 |
} |
|
837 |
||
838 |
/* |
|
839 |
* Close the socket (and release the file descriptor). |
|
840 |
*/ |
|
841 |
protected void socketClose() throws IOException { |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
47216
diff
changeset
|
842 |
SocketCleanable.unregister(fd); |
2 | 843 |
socketClose0(false); |
844 |
} |
|
845 |
||
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
54155
diff
changeset
|
846 |
abstract void socketCreate(boolean stream) throws IOException; |
2 | 847 |
abstract void socketConnect(InetAddress address, int port, int timeout) |
848 |
throws IOException; |
|
849 |
abstract void socketBind(InetAddress address, int port) |
|
850 |
throws IOException; |
|
851 |
abstract void socketListen(int count) |
|
852 |
throws IOException; |
|
853 |
abstract void socketAccept(SocketImpl s) |
|
854 |
throws IOException; |
|
855 |
abstract int socketAvailable() |
|
856 |
throws IOException; |
|
857 |
abstract void socketClose0(boolean useDeferredClose) |
|
858 |
throws IOException; |
|
859 |
abstract void socketShutdown(int howto) |
|
860 |
throws IOException; |
|
861 |
abstract void socketSetOption(int cmd, boolean on, Object value) |
|
862 |
throws SocketException; |
|
863 |
abstract int socketGetOption(int opt, Object iaContainerObj) throws SocketException; |
|
864 |
abstract void socketSendUrgentData(int data) |
|
865 |
throws IOException; |
|
866 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30963
diff
changeset
|
867 |
public static final int SHUT_RD = 0; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30963
diff
changeset
|
868 |
public static final int SHUT_WR = 1; |
36115 | 869 |
|
870 |
private static native boolean isReusePortAvailable0(); |
|
2 | 871 |
} |