author | alanb |
Thu, 15 Mar 2018 11:02:22 +0000 | |
changeset 49249 | 92cca24c8807 |
parent 47216 | 71c04702a3d5 |
child 49833 | 06a6ae39d892 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2007, 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 |
package java.net; |
|
26 |
||
27 |
import java.io.*; |
|
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
28 |
import java.security.AccessController; |
2 | 29 |
import java.security.PrivilegedAction; |
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
30 |
import sun.security.action.GetPropertyAction; |
2 | 31 |
|
32 |
/* |
|
33 |
* This class PlainSocketImpl simply delegates to the appropriate real |
|
34 |
* SocketImpl. We do this because PlainSocketImpl is already extended |
|
35 |
* by SocksSocketImpl. |
|
36 |
* <p> |
|
37 |
* There are two possibilities for the real SocketImpl, |
|
38 |
* TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use |
|
39 |
* DualStackPlainSocketImpl on systems that have a dual stack |
|
40 |
* TCP implementation. Otherwise we create an instance of |
|
41 |
* TwoStacksPlainSocketImpl and delegate to it. |
|
42 |
* |
|
43 |
* @author Chris Hegarty |
|
44 |
*/ |
|
45 |
||
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
47216
diff
changeset
|
46 |
class PlainSocketImpl extends AbstractPlainSocketImpl { |
2 | 47 |
private AbstractPlainSocketImpl impl; |
48 |
||
49 |
/* java.net.preferIPv4Stack */ |
|
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
50 |
private static final boolean preferIPv4Stack; |
18192 | 51 |
|
52 |
/* True if exclusive binding is on for Windows */ |
|
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
53 |
private static final boolean exclusiveBind; |
18192 | 54 |
|
2 | 55 |
static { |
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
56 |
preferIPv4Stack = Boolean.parseBoolean( |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
57 |
AccessController.doPrivileged( |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
58 |
new GetPropertyAction("java.net.preferIPv4Stack"))); |
2 | 59 |
|
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
60 |
String exclBindProp = AccessController.doPrivileged( |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
61 |
new GetPropertyAction("sun.net.useExclusiveBind", "")); |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
62 |
exclusiveBind = (exclBindProp.isEmpty()) |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
63 |
? true |
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
64 |
: Boolean.parseBoolean(exclBindProp); |
2 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Constructs an empty instance. |
|
69 |
*/ |
|
70 |
PlainSocketImpl() { |
|
37368
bf9b868f1aa8
8154238: Drop code to support Windows XP in windows socket impl
redestad
parents:
36115
diff
changeset
|
71 |
if (!preferIPv4Stack) { |
18192 | 72 |
impl = new DualStackPlainSocketImpl(exclusiveBind); |
2 | 73 |
} else { |
18192 | 74 |
impl = new TwoStacksPlainSocketImpl(exclusiveBind); |
2 | 75 |
} |
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Constructs an instance with the given file descriptor. |
|
80 |
*/ |
|
81 |
PlainSocketImpl(FileDescriptor fd) { |
|
37370
4dcebdd2bbc0
8154454: Fix compilation issue in PlainSocketImpl
redestad
parents:
37368
diff
changeset
|
82 |
if (!preferIPv4Stack) { |
18192 | 83 |
impl = new DualStackPlainSocketImpl(fd, exclusiveBind); |
2 | 84 |
} else { |
18192 | 85 |
impl = new TwoStacksPlainSocketImpl(fd, exclusiveBind); |
2 | 86 |
} |
87 |
} |
|
88 |
||
89 |
// Override methods in SocketImpl that access impl's fields. |
|
90 |
||
91 |
protected FileDescriptor getFileDescriptor() { |
|
92 |
return impl.getFileDescriptor(); |
|
93 |
} |
|
94 |
||
95 |
protected InetAddress getInetAddress() { |
|
96 |
return impl.getInetAddress(); |
|
97 |
} |
|
98 |
||
99 |
protected int getPort() { |
|
100 |
return impl.getPort(); |
|
101 |
} |
|
102 |
||
103 |
protected int getLocalPort() { |
|
104 |
return impl.getLocalPort(); |
|
105 |
} |
|
106 |
||
107 |
void setSocket(Socket soc) { |
|
108 |
impl.setSocket(soc); |
|
109 |
} |
|
110 |
||
111 |
Socket getSocket() { |
|
112 |
return impl.getSocket(); |
|
113 |
} |
|
114 |
||
115 |
void setServerSocket(ServerSocket soc) { |
|
116 |
impl.setServerSocket(soc); |
|
117 |
} |
|
118 |
||
119 |
ServerSocket getServerSocket() { |
|
120 |
return impl.getServerSocket(); |
|
121 |
} |
|
122 |
||
123 |
public String toString() { |
|
124 |
return impl.toString(); |
|
125 |
} |
|
126 |
||
127 |
// Override methods in AbstractPlainSocketImpl that access impl's fields. |
|
128 |
||
129 |
protected synchronized void create(boolean stream) throws IOException { |
|
130 |
impl.create(stream); |
|
8185 | 131 |
|
132 |
// set fd to delegate's fd to be compatible with older releases |
|
133 |
this.fd = impl.fd; |
|
2 | 134 |
} |
135 |
||
136 |
protected void connect(String host, int port) |
|
137 |
throws UnknownHostException, IOException |
|
138 |
{ |
|
139 |
impl.connect(host, port); |
|
140 |
} |
|
141 |
||
142 |
protected void connect(InetAddress address, int port) throws IOException { |
|
143 |
impl.connect(address, port); |
|
144 |
} |
|
145 |
||
146 |
protected void connect(SocketAddress address, int timeout) throws IOException { |
|
147 |
impl.connect(address, timeout); |
|
148 |
} |
|
149 |
||
150 |
public void setOption(int opt, Object val) throws SocketException { |
|
36115 | 151 |
if (opt == SocketOptions.SO_REUSEPORT) { |
152 |
// SO_REUSEPORT is not supported on Windows. |
|
153 |
throw new UnsupportedOperationException("unsupported option"); |
|
154 |
} |
|
2 | 155 |
impl.setOption(opt, val); |
156 |
} |
|
157 |
||
158 |
public Object getOption(int opt) throws SocketException { |
|
36115 | 159 |
if (opt == SocketOptions.SO_REUSEPORT) { |
160 |
// SO_REUSEPORT is not supported on Windows. |
|
161 |
throw new UnsupportedOperationException("unsupported option"); |
|
162 |
} |
|
2 | 163 |
return impl.getOption(opt); |
164 |
} |
|
165 |
||
166 |
synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException { |
|
167 |
impl.doConnect(address, port, timeout); |
|
168 |
} |
|
169 |
||
8185 | 170 |
protected synchronized void bind(InetAddress address, int lport) |
2 | 171 |
throws IOException |
172 |
{ |
|
173 |
impl.bind(address, lport); |
|
174 |
} |
|
175 |
||
176 |
protected synchronized void accept(SocketImpl s) throws IOException { |
|
20501
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
177 |
if (s instanceof PlainSocketImpl) { |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
178 |
// pass in the real impl not the wrapper. |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
179 |
SocketImpl delegate = ((PlainSocketImpl)s).impl; |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
180 |
delegate.address = new InetAddress(); |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
181 |
delegate.fd = new FileDescriptor(); |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
182 |
impl.accept(delegate); |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
183 |
// set fd to delegate's fd to be compatible with older releases |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
184 |
s.fd = delegate.fd; |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
185 |
} else { |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
186 |
impl.accept(s); |
058de8b0a499
8024952: ClassCastException in PlainSocketImpl.accept() when using custom socketImpl
coffeys
parents:
18192
diff
changeset
|
187 |
} |
2 | 188 |
} |
189 |
||
190 |
void setFileDescriptor(FileDescriptor fd) { |
|
191 |
impl.setFileDescriptor(fd); |
|
192 |
} |
|
193 |
||
194 |
void setAddress(InetAddress address) { |
|
195 |
impl.setAddress(address); |
|
196 |
} |
|
197 |
||
198 |
void setPort(int port) { |
|
199 |
impl.setPort(port); |
|
200 |
} |
|
201 |
||
202 |
void setLocalPort(int localPort) { |
|
203 |
impl.setLocalPort(localPort); |
|
204 |
} |
|
205 |
||
206 |
protected synchronized InputStream getInputStream() throws IOException { |
|
207 |
return impl.getInputStream(); |
|
208 |
} |
|
209 |
||
210 |
void setInputStream(SocketInputStream in) { |
|
211 |
impl.setInputStream(in); |
|
212 |
} |
|
213 |
||
214 |
protected synchronized OutputStream getOutputStream() throws IOException { |
|
215 |
return impl.getOutputStream(); |
|
216 |
} |
|
217 |
||
218 |
protected void close() throws IOException { |
|
8185 | 219 |
try { |
220 |
impl.close(); |
|
221 |
} finally { |
|
222 |
// set fd to delegate's fd to be compatible with older releases |
|
223 |
this.fd = null; |
|
224 |
} |
|
2 | 225 |
} |
226 |
||
227 |
void reset() throws IOException { |
|
8185 | 228 |
try { |
229 |
impl.reset(); |
|
230 |
} finally { |
|
231 |
// set fd to delegate's fd to be compatible with older releases |
|
232 |
this.fd = null; |
|
233 |
} |
|
2 | 234 |
} |
235 |
||
236 |
protected void shutdownInput() throws IOException { |
|
237 |
impl.shutdownInput(); |
|
238 |
} |
|
239 |
||
240 |
protected void shutdownOutput() throws IOException { |
|
241 |
impl.shutdownOutput(); |
|
242 |
} |
|
243 |
||
244 |
protected void sendUrgentData(int data) throws IOException { |
|
245 |
impl.sendUrgentData(data); |
|
246 |
} |
|
247 |
||
248 |
FileDescriptor acquireFD() { |
|
249 |
return impl.acquireFD(); |
|
250 |
} |
|
251 |
||
252 |
void releaseFD() { |
|
253 |
impl.releaseFD(); |
|
254 |
} |
|
255 |
||
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
47216
diff
changeset
|
256 |
boolean isConnectionReset() { |
2 | 257 |
return impl.isConnectionReset(); |
258 |
} |
|
259 |
||
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
47216
diff
changeset
|
260 |
void setConnectionReset() { |
2 | 261 |
impl.setConnectionReset(); |
262 |
} |
|
263 |
||
264 |
public boolean isClosedOrPending() { |
|
265 |
return impl.isClosedOrPending(); |
|
266 |
} |
|
267 |
||
268 |
public int getTimeout() { |
|
269 |
return impl.getTimeout(); |
|
270 |
} |
|
271 |
||
272 |
// Override methods in AbstractPlainSocketImpl that need to be implemented. |
|
273 |
||
274 |
void socketCreate(boolean isServer) throws IOException { |
|
275 |
impl.socketCreate(isServer); |
|
276 |
} |
|
277 |
||
278 |
void socketConnect(InetAddress address, int port, int timeout) |
|
279 |
throws IOException { |
|
280 |
impl.socketConnect(address, port, timeout); |
|
281 |
} |
|
282 |
||
283 |
void socketBind(InetAddress address, int port) |
|
284 |
throws IOException { |
|
285 |
impl.socketBind(address, port); |
|
286 |
} |
|
287 |
||
288 |
void socketListen(int count) throws IOException { |
|
289 |
impl.socketListen(count); |
|
290 |
} |
|
291 |
||
292 |
void socketAccept(SocketImpl s) throws IOException { |
|
293 |
impl.socketAccept(s); |
|
294 |
} |
|
295 |
||
296 |
int socketAvailable() throws IOException { |
|
297 |
return impl.socketAvailable(); |
|
298 |
} |
|
299 |
||
300 |
void socketClose0(boolean useDeferredClose) throws IOException { |
|
301 |
impl.socketClose0(useDeferredClose); |
|
302 |
} |
|
303 |
||
304 |
void socketShutdown(int howto) throws IOException { |
|
305 |
impl.socketShutdown(howto); |
|
306 |
} |
|
307 |
||
308 |
void socketSetOption(int cmd, boolean on, Object value) |
|
309 |
throws SocketException { |
|
36115 | 310 |
if (cmd == SocketOptions.SO_REUSEPORT) { |
311 |
// SO_REUSEPORT is not supported on Windows. |
|
312 |
throw new UnsupportedOperationException("unsupported option"); |
|
313 |
} |
|
11019
aa969c520286
7107020: java.net.PlainSocketImpl.socketSetOption() calls itself
chegar
parents:
9035
diff
changeset
|
314 |
impl.socketSetOption(cmd, on, value); |
2 | 315 |
} |
316 |
||
317 |
int socketGetOption(int opt, Object iaContainerObj) throws SocketException { |
|
36115 | 318 |
if (opt == SocketOptions.SO_REUSEPORT) { |
319 |
// SO_REUSEPORT is not supported on Windows. |
|
320 |
throw new UnsupportedOperationException("unsupported option"); |
|
321 |
} |
|
2 | 322 |
return impl.socketGetOption(opt, iaContainerObj); |
323 |
} |
|
324 |
||
325 |
void socketSendUrgentData(int data) throws IOException { |
|
326 |
impl.socketSendUrgentData(data); |
|
327 |
} |
|
36115 | 328 |
|
329 |
static boolean isReusePortAvailable() { |
|
330 |
// SO_REUSEPORT is not supported on Windows. |
|
331 |
return false; |
|
332 |
} |
|
2 | 333 |
} |