blob: c65c71c670f9a192a4c9bac8dd2b8a89aab8b6e3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package java.net;
26
27import java.io.*;
28import 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
44class 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
57 static {
58 java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {
59 public Object run() {
60 version = 0;
61 try {
62 version = Float.parseFloat(System.getProperties().getProperty("os.version"));
63 preferIPv4Stack = Boolean.parseBoolean(
64 System.getProperties().getProperty("java.net.preferIPv4Stack"));
65 } catch (NumberFormatException e ) {
66 assert false : e;
67 }
68 return null; // nothing to return
69 } });
70
71 // (version >= 6.0) implies Vista or greater.
72 if (version >= 6.0 && !preferIPv4Stack) {
73 useDualStackImpl = true;
74 }
75 }
76
77 /**
78 * Constructs an empty instance.
79 */
80 PlainSocketImpl() {
81 if (useDualStackImpl) {
82 impl = new DualStackPlainSocketImpl();
83 } else {
84 impl = new TwoStacksPlainSocketImpl();
85 }
86 }
87
88 /**
89 * Constructs an instance with the given file descriptor.
90 */
91 PlainSocketImpl(FileDescriptor fd) {
92 if (useDualStackImpl) {
93 impl = new DualStackPlainSocketImpl(fd);
94 } else {
95 impl = new TwoStacksPlainSocketImpl(fd);
96 }
97 }
98
99 // Override methods in SocketImpl that access impl's fields.
100
101 protected FileDescriptor getFileDescriptor() {
102 return impl.getFileDescriptor();
103 }
104
105 protected InetAddress getInetAddress() {
106 return impl.getInetAddress();
107 }
108
109 protected int getPort() {
110 return impl.getPort();
111 }
112
113 protected int getLocalPort() {
114 return impl.getLocalPort();
115 }
116
117 void setSocket(Socket soc) {
118 impl.setSocket(soc);
119 }
120
121 Socket getSocket() {
122 return impl.getSocket();
123 }
124
125 void setServerSocket(ServerSocket soc) {
126 impl.setServerSocket(soc);
127 }
128
129 ServerSocket getServerSocket() {
130 return impl.getServerSocket();
131 }
132
133 public String toString() {
134 return impl.toString();
135 }
136
137 // Override methods in AbstractPlainSocketImpl that access impl's fields.
138
139 protected synchronized void create(boolean stream) throws IOException {
140 impl.create(stream);
141 }
142
143 protected void connect(String host, int port)
144 throws UnknownHostException, IOException
145 {
146 impl.connect(host, port);
147 }
148
149 protected void connect(InetAddress address, int port) throws IOException {
150 impl.connect(address, port);
151 }
152
153 protected void connect(SocketAddress address, int timeout) throws IOException {
154 impl.connect(address, timeout);
155 }
156
157 public void setOption(int opt, Object val) throws SocketException {
158 impl.setOption(opt, val);
159 }
160
161 public Object getOption(int opt) throws SocketException {
162 return impl.getOption(opt);
163 }
164
165 synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
166 impl.doConnect(address, port, timeout);
167 }
168
169 protected synchronized void bind(InetAddress address, int lport)
170 throws IOException
171 {
172 impl.bind(address, lport);
173 }
174
175 protected synchronized void accept(SocketImpl s) throws IOException {
176 // pass in the real impl not the wrapper.
177 ((PlainSocketImpl)s).impl.address = new InetAddress();
178 ((PlainSocketImpl)s).impl.fd = new FileDescriptor();
179 impl.accept(((PlainSocketImpl)s).impl);
180 }
181
182 void setFileDescriptor(FileDescriptor fd) {
183 impl.setFileDescriptor(fd);
184 }
185
186 void setAddress(InetAddress address) {
187 impl.setAddress(address);
188 }
189
190 void setPort(int port) {
191 impl.setPort(port);
192 }
193
194 void setLocalPort(int localPort) {
195 impl.setLocalPort(localPort);
196 }
197
198 protected synchronized InputStream getInputStream() throws IOException {
199 return impl.getInputStream();
200 }
201
202 void setInputStream(SocketInputStream in) {
203 impl.setInputStream(in);
204 }
205
206 protected synchronized OutputStream getOutputStream() throws IOException {
207 return impl.getOutputStream();
208 }
209
210 protected void close() throws IOException {
211 impl.close();
212 }
213
214 void reset() throws IOException {
215 impl.reset();
216 }
217
218 protected void shutdownInput() throws IOException {
219 impl.shutdownInput();
220 }
221
222 protected void shutdownOutput() throws IOException {
223 impl.shutdownOutput();
224 }
225
226 protected void sendUrgentData(int data) throws IOException {
227 impl.sendUrgentData(data);
228 }
229
230 FileDescriptor acquireFD() {
231 return impl.acquireFD();
232 }
233
234 void releaseFD() {
235 impl.releaseFD();
236 }
237
238 public boolean isConnectionReset() {
239 return impl.isConnectionReset();
240 }
241
242 public boolean isConnectionResetPending() {
243 return impl.isConnectionResetPending();
244 }
245
246 public void setConnectionReset() {
247 impl.setConnectionReset();
248 }
249
250 public void setConnectionResetPending() {
251 impl.setConnectionResetPending();
252 }
253
254 public boolean isClosedOrPending() {
255 return impl.isClosedOrPending();
256 }
257
258 public int getTimeout() {
259 return impl.getTimeout();
260 }
261
262 // Override methods in AbstractPlainSocketImpl that need to be implemented.
263
264 void socketCreate(boolean isServer) throws IOException {
265 impl.socketCreate(isServer);
266 }
267
268 void socketConnect(InetAddress address, int port, int timeout)
269 throws IOException {
270 impl.socketConnect(address, port, timeout);
271 }
272
273 void socketBind(InetAddress address, int port)
274 throws IOException {
275 impl.socketBind(address, port);
276 }
277
278 void socketListen(int count) throws IOException {
279 impl.socketListen(count);
280 }
281
282 void socketAccept(SocketImpl s) throws IOException {
283 impl.socketAccept(s);
284 }
285
286 int socketAvailable() throws IOException {
287 return impl.socketAvailable();
288 }
289
290 void socketClose0(boolean useDeferredClose) throws IOException {
291 impl.socketClose0(useDeferredClose);
292 }
293
294 void socketShutdown(int howto) throws IOException {
295 impl.socketShutdown(howto);
296 }
297
298 void socketSetOption(int cmd, boolean on, Object value)
299 throws SocketException {
300 socketSetOption(cmd, on, value);
301 }
302
303 int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
304 return impl.socketGetOption(opt, iaContainerObj);
305 }
306
307 int socketGetOption1(int opt, Object iaContainerObj, FileDescriptor fd)
308 throws SocketException {
309 return impl.socketGetOption1(opt, iaContainerObj, fd);
310 }
311
312 void socketSendUrgentData(int data) throws IOException {
313 impl.socketSendUrgentData(data);
314 }
315}