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