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