8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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.
24 */
25 package java.net;
26
27 import java.io.*;
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import sun.security.action.GetPropertyAction;
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
46 class PlainSocketImpl extends AbstractPlainSocketImpl {
47 private AbstractPlainSocketImpl impl;
48
49 /* java.net.preferIPv4Stack */
50 private static final boolean preferIPv4Stack;
51
52 /* True if exclusive binding is on for Windows */
53 private static final boolean exclusiveBind;
54
55 static {
56 preferIPv4Stack = Boolean.parseBoolean(
57 AccessController.doPrivileged(
58 new GetPropertyAction("java.net.preferIPv4Stack")));
59
60 String exclBindProp = AccessController.doPrivileged(
61 new GetPropertyAction("sun.net.useExclusiveBind", ""));
62 exclusiveBind = (exclBindProp.isEmpty())
63 ? true
64 : Boolean.parseBoolean(exclBindProp);
65 }
66
67 /**
68 * Constructs an empty instance.
69 */
70 PlainSocketImpl() {
71 if (!preferIPv4Stack) {
72 impl = new DualStackPlainSocketImpl(exclusiveBind);
73 } else {
74 impl = new TwoStacksPlainSocketImpl(exclusiveBind);
75 }
76 }
77
78 /**
79 * Constructs an instance with the given file descriptor.
80 */
81 PlainSocketImpl(FileDescriptor fd) {
82 if (!preferIPv4Stack) {
83 impl = new DualStackPlainSocketImpl(fd, exclusiveBind);
84 } else {
85 impl = new TwoStacksPlainSocketImpl(fd, exclusiveBind);
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
131
132 // set fd to delegate's fd to be compatible with older releases
133 this.fd = impl.fd;
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 {
151 if (opt == SocketOptions.SO_REUSEPORT) {
152 // SO_REUSEPORT is not supported on Windows.
153 throw new UnsupportedOperationException("unsupported option");
154 }
155 impl.setOption(opt, val);
156 }
157
158 public Object getOption(int opt) throws SocketException {
159 if (opt == SocketOptions.SO_REUSEPORT) {
160 // SO_REUSEPORT is not supported on Windows.
161 throw new UnsupportedOperationException("unsupported option");
162 }
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
170 protected synchronized void bind(InetAddress address, int lport)
171 throws IOException
172 {
173 impl.bind(address, lport);
174 }
175
176 protected synchronized void accept(SocketImpl s) throws IOException {
177 if (s instanceof PlainSocketImpl) {
178 // pass in the real impl not the wrapper.
179 SocketImpl delegate = ((PlainSocketImpl)s).impl;
180 delegate.address = new InetAddress();
181 delegate.fd = new FileDescriptor();
182 impl.accept(delegate);
254 }
255
256 boolean isConnectionReset() {
257 return impl.isConnectionReset();
258 }
259
260 void setConnectionReset() {
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 {
310 if (cmd == SocketOptions.SO_REUSEPORT) {
311 // SO_REUSEPORT is not supported on Windows.
312 throw new UnsupportedOperationException("unsupported option");
313 }
314 impl.socketSetOption(cmd, on, value);
315 }
316
317 int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
318 if (opt == SocketOptions.SO_REUSEPORT) {
319 // SO_REUSEPORT is not supported on Windows.
320 throw new UnsupportedOperationException("unsupported option");
321 }
322 return impl.socketGetOption(opt, iaContainerObj);
323 }
324
325 void socketSendUrgentData(int data) throws IOException {
326 impl.socketSendUrgentData(data);
327 }
328
329 static boolean isReusePortAvailable() {
330 // SO_REUSEPORT is not supported on Windows.
331 return false;
332 }
333 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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.
24 */
25 package java.net;
26
27 import java.io.*;
28
29 /*
30 * This class PlainSocketImpl simply delegates to the appropriate real
31 * SocketImpl. We do this because PlainSocketImpl is already extended
32 * by SocksSocketImpl.
33 * <p>
34 * There is one possibility for the real SocketImpl: DualStackPlainSocketImpl.
35 *
36 * @author Chris Hegarty
37 */
38
39 class PlainSocketImpl extends AbstractPlainSocketImpl {
40 private AbstractPlainSocketImpl impl;
41
42 /**
43 * Constructs an empty instance.
44 */
45 PlainSocketImpl() {
46 impl = new DualStackPlainSocketImpl();
47 }
48
49 /**
50 * Constructs an instance with the given file descriptor.
51 */
52 PlainSocketImpl(FileDescriptor fd) {
53 impl = new DualStackPlainSocketImpl(fd);
54 }
55
56 // Override methods in SocketImpl that access impl's fields.
57
58 protected FileDescriptor getFileDescriptor() {
59 return impl.getFileDescriptor();
60 }
61
62 protected InetAddress getInetAddress() {
63 return impl.getInetAddress();
64 }
65
66 protected int getPort() {
67 return impl.getPort();
68 }
69
70 protected int getLocalPort() {
71 return impl.getLocalPort();
72 }
73
98
99 // set fd to delegate's fd to be compatible with older releases
100 this.fd = impl.fd;
101 }
102
103 protected void connect(String host, int port)
104 throws UnknownHostException, IOException
105 {
106 impl.connect(host, port);
107 }
108
109 protected void connect(InetAddress address, int port) throws IOException {
110 impl.connect(address, port);
111 }
112
113 protected void connect(SocketAddress address, int timeout) throws IOException {
114 impl.connect(address, timeout);
115 }
116
117 public void setOption(int opt, Object val) throws SocketException {
118 impl.setOption(opt, val);
119 }
120
121 public Object getOption(int opt) throws SocketException {
122 return impl.getOption(opt);
123 }
124
125 synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
126 impl.doConnect(address, port, timeout);
127 }
128
129 protected synchronized void bind(InetAddress address, int lport)
130 throws IOException
131 {
132 impl.bind(address, lport);
133 }
134
135 protected synchronized void accept(SocketImpl s) throws IOException {
136 if (s instanceof PlainSocketImpl) {
137 // pass in the real impl not the wrapper.
138 SocketImpl delegate = ((PlainSocketImpl)s).impl;
139 delegate.address = new InetAddress();
140 delegate.fd = new FileDescriptor();
141 impl.accept(delegate);
213 }
214
215 boolean isConnectionReset() {
216 return impl.isConnectionReset();
217 }
218
219 void setConnectionReset() {
220 impl.setConnectionReset();
221 }
222
223 public boolean isClosedOrPending() {
224 return impl.isClosedOrPending();
225 }
226
227 public int getTimeout() {
228 return impl.getTimeout();
229 }
230
231 // Override methods in AbstractPlainSocketImpl that need to be implemented.
232
233 void socketCreate(boolean stream) throws IOException {
234 impl.socketCreate(stream);
235 }
236
237 void socketConnect(InetAddress address, int port, int timeout)
238 throws IOException {
239 impl.socketConnect(address, port, timeout);
240 }
241
242 void socketBind(InetAddress address, int port)
243 throws IOException {
244 impl.socketBind(address, port);
245 }
246
247 void socketListen(int count) throws IOException {
248 impl.socketListen(count);
249 }
250
251 void socketAccept(SocketImpl s) throws IOException {
252 impl.socketAccept(s);
253 }
254
255 int socketAvailable() throws IOException {
256 return impl.socketAvailable();
257 }
258
259 void socketClose0(boolean useDeferredClose) throws IOException {
260 impl.socketClose0(useDeferredClose);
261 }
262
263 void socketShutdown(int howto) throws IOException {
264 impl.socketShutdown(howto);
265 }
266
267 void socketSetOption(int cmd, boolean on, Object value)
268 throws SocketException {
269 impl.socketSetOption(cmd, on, value);
270 }
271
272 int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
273 return impl.socketGetOption(opt, iaContainerObj);
274 }
275
276 void socketSendUrgentData(int data) throws IOException {
277 impl.socketSendUrgentData(data);
278 }
279
280 static boolean isReusePortAvailable() {
281 // SO_REUSEPORT is not supported on Windows.
282 return false;
283 }
284 }
|