0N/A/*
3909N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.net;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.security.PrivilegedAction;
0N/A
0N/A/*
0N/A * This class PlainSocketImpl simply delegates to the appropriate real
0N/A * SocketImpl. We do this because PlainSocketImpl is already extended
0N/A * by SocksSocketImpl.
0N/A * <p>
0N/A * There are two possibilities for the real SocketImpl,
0N/A * TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use
0N/A * DualStackPlainSocketImpl on systems that have a dual stack
0N/A * TCP implementation. Otherwise we create an instance of
0N/A * TwoStacksPlainSocketImpl and delegate to it.
0N/A *
0N/A * @author Chris Hegarty
0N/A */
0N/A
0N/Aclass PlainSocketImpl extends AbstractPlainSocketImpl
0N/A{
0N/A private AbstractPlainSocketImpl impl;
0N/A
0N/A /* the windows version. */
0N/A private static float version;
0N/A
0N/A /* java.net.preferIPv4Stack */
0N/A private static boolean preferIPv4Stack = false;
0N/A
0N/A /* If the version supports a dual stack TCP implementation */
0N/A private static boolean useDualStackImpl = false;
0N/A
6272N/A /* sun.net.useExclusiveBind */
6272N/A private static String exclBindProp;
6272N/A
6272N/A /* True if exclusive binding is on for Windows */
6272N/A private static boolean exclusiveBind = true;
6272N/A
0N/A static {
0N/A java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {
0N/A public Object run() {
0N/A version = 0;
0N/A try {
0N/A version = Float.parseFloat(System.getProperties().getProperty("os.version"));
0N/A preferIPv4Stack = Boolean.parseBoolean(
0N/A System.getProperties().getProperty("java.net.preferIPv4Stack"));
6272N/A exclBindProp = System.getProperty("sun.net.useExclusiveBind");
0N/A } catch (NumberFormatException e ) {
0N/A assert false : e;
0N/A }
0N/A return null; // nothing to return
0N/A } });
0N/A
0N/A // (version >= 6.0) implies Vista or greater.
0N/A if (version >= 6.0 && !preferIPv4Stack) {
6272N/A useDualStackImpl = true;
6272N/A }
6272N/A
6272N/A if (exclBindProp != null) {
6272N/A // sun.net.useExclusiveBind is true
6272N/A exclusiveBind = exclBindProp.length() == 0 ? true
6272N/A : Boolean.parseBoolean(exclBindProp);
6272N/A } else if (version < 6.0) {
6272N/A exclusiveBind = false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty instance.
0N/A */
0N/A PlainSocketImpl() {
0N/A if (useDualStackImpl) {
6272N/A impl = new DualStackPlainSocketImpl(exclusiveBind);
0N/A } else {
6272N/A impl = new TwoStacksPlainSocketImpl(exclusiveBind);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs an instance with the given file descriptor.
0N/A */
0N/A PlainSocketImpl(FileDescriptor fd) {
0N/A if (useDualStackImpl) {
6272N/A impl = new DualStackPlainSocketImpl(fd, exclusiveBind);
0N/A } else {
6272N/A impl = new TwoStacksPlainSocketImpl(fd, exclusiveBind);
0N/A }
0N/A }
0N/A
0N/A // Override methods in SocketImpl that access impl's fields.
0N/A
0N/A protected FileDescriptor getFileDescriptor() {
0N/A return impl.getFileDescriptor();
0N/A }
0N/A
0N/A protected InetAddress getInetAddress() {
0N/A return impl.getInetAddress();
0N/A }
0N/A
0N/A protected int getPort() {
0N/A return impl.getPort();
0N/A }
0N/A
0N/A protected int getLocalPort() {
0N/A return impl.getLocalPort();
0N/A }
0N/A
0N/A void setSocket(Socket soc) {
0N/A impl.setSocket(soc);
0N/A }
0N/A
0N/A Socket getSocket() {
0N/A return impl.getSocket();
0N/A }
0N/A
0N/A void setServerSocket(ServerSocket soc) {
0N/A impl.setServerSocket(soc);
0N/A }
0N/A
0N/A ServerSocket getServerSocket() {
0N/A return impl.getServerSocket();
0N/A }
0N/A
0N/A public String toString() {
0N/A return impl.toString();
0N/A }
0N/A
0N/A // Override methods in AbstractPlainSocketImpl that access impl's fields.
0N/A
0N/A protected synchronized void create(boolean stream) throws IOException {
0N/A impl.create(stream);
3498N/A
3498N/A // set fd to delegate's fd to be compatible with older releases
3498N/A this.fd = impl.fd;
0N/A }
0N/A
0N/A protected void connect(String host, int port)
0N/A throws UnknownHostException, IOException
0N/A {
0N/A impl.connect(host, port);
0N/A }
0N/A
0N/A protected void connect(InetAddress address, int port) throws IOException {
0N/A impl.connect(address, port);
0N/A }
0N/A
0N/A protected void connect(SocketAddress address, int timeout) throws IOException {
0N/A impl.connect(address, timeout);
0N/A }
0N/A
0N/A public void setOption(int opt, Object val) throws SocketException {
0N/A impl.setOption(opt, val);
0N/A }
0N/A
0N/A public Object getOption(int opt) throws SocketException {
0N/A return impl.getOption(opt);
0N/A }
0N/A
0N/A synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException {
0N/A impl.doConnect(address, port, timeout);
0N/A }
0N/A
3498N/A protected synchronized void bind(InetAddress address, int lport)
0N/A throws IOException
0N/A {
0N/A impl.bind(address, lport);
0N/A }
0N/A
0N/A protected synchronized void accept(SocketImpl s) throws IOException {
0N/A // pass in the real impl not the wrapper.
3498N/A SocketImpl delegate = ((PlainSocketImpl)s).impl;
3498N/A delegate.address = new InetAddress();
3498N/A delegate.fd = new FileDescriptor();
3498N/A impl.accept(delegate);
3498N/A
3498N/A // set fd to delegate's fd to be compatible with older releases
3498N/A s.fd = delegate.fd;
0N/A }
0N/A
0N/A void setFileDescriptor(FileDescriptor fd) {
0N/A impl.setFileDescriptor(fd);
0N/A }
0N/A
0N/A void setAddress(InetAddress address) {
0N/A impl.setAddress(address);
0N/A }
0N/A
0N/A void setPort(int port) {
0N/A impl.setPort(port);
0N/A }
0N/A
0N/A void setLocalPort(int localPort) {
0N/A impl.setLocalPort(localPort);
0N/A }
0N/A
0N/A protected synchronized InputStream getInputStream() throws IOException {
0N/A return impl.getInputStream();
0N/A }
0N/A
0N/A void setInputStream(SocketInputStream in) {
0N/A impl.setInputStream(in);
0N/A }
0N/A
0N/A protected synchronized OutputStream getOutputStream() throws IOException {
0N/A return impl.getOutputStream();
0N/A }
0N/A
0N/A protected void close() throws IOException {
3498N/A try {
3498N/A impl.close();
3498N/A } finally {
3498N/A // set fd to delegate's fd to be compatible with older releases
3498N/A this.fd = null;
3498N/A }
0N/A }
0N/A
0N/A void reset() throws IOException {
3498N/A try {
3498N/A impl.reset();
3498N/A } finally {
3498N/A // set fd to delegate's fd to be compatible with older releases
3498N/A this.fd = null;
3498N/A }
0N/A }
0N/A
0N/A protected void shutdownInput() throws IOException {
0N/A impl.shutdownInput();
0N/A }
0N/A
0N/A protected void shutdownOutput() throws IOException {
0N/A impl.shutdownOutput();
0N/A }
0N/A
0N/A protected void sendUrgentData(int data) throws IOException {
0N/A impl.sendUrgentData(data);
0N/A }
0N/A
0N/A FileDescriptor acquireFD() {
0N/A return impl.acquireFD();
0N/A }
0N/A
0N/A void releaseFD() {
0N/A impl.releaseFD();
0N/A }
0N/A
0N/A public boolean isConnectionReset() {
0N/A return impl.isConnectionReset();
0N/A }
0N/A
0N/A public boolean isConnectionResetPending() {
0N/A return impl.isConnectionResetPending();
0N/A }
0N/A
0N/A public void setConnectionReset() {
0N/A impl.setConnectionReset();
0N/A }
0N/A
0N/A public void setConnectionResetPending() {
0N/A impl.setConnectionResetPending();
0N/A }
0N/A
0N/A public boolean isClosedOrPending() {
0N/A return impl.isClosedOrPending();
0N/A }
0N/A
0N/A public int getTimeout() {
0N/A return impl.getTimeout();
0N/A }
0N/A
0N/A // Override methods in AbstractPlainSocketImpl that need to be implemented.
0N/A
0N/A void socketCreate(boolean isServer) throws IOException {
0N/A impl.socketCreate(isServer);
0N/A }
0N/A
0N/A void socketConnect(InetAddress address, int port, int timeout)
0N/A throws IOException {
0N/A impl.socketConnect(address, port, timeout);
0N/A }
0N/A
0N/A void socketBind(InetAddress address, int port)
0N/A throws IOException {
0N/A impl.socketBind(address, port);
0N/A }
0N/A
0N/A void socketListen(int count) throws IOException {
0N/A impl.socketListen(count);
0N/A }
0N/A
0N/A void socketAccept(SocketImpl s) throws IOException {
0N/A impl.socketAccept(s);
0N/A }
0N/A
0N/A int socketAvailable() throws IOException {
0N/A return impl.socketAvailable();
0N/A }
0N/A
0N/A void socketClose0(boolean useDeferredClose) throws IOException {
0N/A impl.socketClose0(useDeferredClose);
0N/A }
0N/A
0N/A void socketShutdown(int howto) throws IOException {
0N/A impl.socketShutdown(howto);
0N/A }
0N/A
0N/A void socketSetOption(int cmd, boolean on, Object value)
0N/A throws SocketException {
0N/A socketSetOption(cmd, on, value);
0N/A }
0N/A
0N/A int socketGetOption(int opt, Object iaContainerObj) throws SocketException {
0N/A return impl.socketGetOption(opt, iaContainerObj);
0N/A }
0N/A
0N/A void socketSendUrgentData(int data) throws IOException {
0N/A impl.socketSendUrgentData(data);
0N/A }
0N/A}