src/java.base/share/classes/java/net/SocksSocketImpl.java
author alanb
Wed, 23 Jan 2019 19:30:59 +0000
branchniosocketimpl-branch
changeset 57110 b848ca1ef778
parent 52499 768b1c612100
child 57112 d7b54daf5e1a
permissions -rw-r--r--
Prototype of NIO based SocketImpl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
     2
 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5147
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessController;
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
    31
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedExceptionAction;
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
    33
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
    34
import jdk.internal.util.StaticProperty;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 7668
diff changeset
    35
import sun.net.SocksProxy;
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    36
import sun.net.spi.DefaultProxySelector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.www.ParseUtil;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
    38
import sun.nio.ch.NioSocketImpl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/* import org.ietf.jgss.*; */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Note this class should <b>NOT</b> be public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
    46
class SocksSocketImpl extends NioSocketImpl implements SocksConsts {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private String server = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
    48
    private int serverPort = DEFAULT_PORT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private InetSocketAddress external_address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean useV4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Socket cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private InputStream cmdIn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private OutputStream cmdOut = null;
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
    54
    /* true if the Proxy has been set programmatically */
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
    55
    private boolean applicationSetProxy;  /* false */
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    SocksSocketImpl() {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
    58
        super(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    SocksSocketImpl(Proxy proxy) {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
    62
        super(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        SocketAddress a = proxy.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (a instanceof InetSocketAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            InetSocketAddress ad = (InetSocketAddress) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            server = ad.getHostString();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
    68
            serverPort = ad.getPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    70
        useV4 = useV4(proxy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void setV4() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        useV4 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    77
    private static boolean useV4(Proxy proxy) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    78
        if (proxy instanceof SocksProxy
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    79
            && ((SocksProxy)proxy).protocolVersion() == 4) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    80
            return true;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    81
        }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    82
        return DefaultProxySelector.socksProxyVersion() == 4;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    83
    }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private synchronized void privilegedConnect(final String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                              final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                              final int timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
    92
                new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    93
                    public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                              superConnectServer(host, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                              cmdIn = getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                              cmdOut = getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                              return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                      });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw (IOException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void superConnectServer(String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                    int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        super.connect(new InetSocketAddress(host, port), timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   110
    private static int remainingMillis(long deadlineMillis) throws IOException {
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   111
        if (deadlineMillis == 0L)
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   112
            return 0;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   113
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   114
        final long remaining = deadlineMillis - System.currentTimeMillis();
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   115
        if (remaining > 0)
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   116
            return (int) remaining;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   117
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   118
        throw new SocketTimeoutException();
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   119
    }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   120
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private int readSocksReply(InputStream in, byte[] data) throws IOException {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   122
        return readSocksReply(in, data, 0L);
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   123
    }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   124
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   125
    private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int len = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int received = 0;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   128
        int originalTimeout = (int) getOption(SocketOptions.SO_TIMEOUT);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   129
        try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   130
            while (received < len) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   131
                int count;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   132
                int remaining = remainingMillis(deadlineMillis);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   133
                setOption(SocketOptions.SO_TIMEOUT, remaining);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   134
                try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   135
                    count = in.read(data, received, len - received);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   136
                } catch (SocketTimeoutException e) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   137
                    throw new SocketTimeoutException("Connect timed out");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   138
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   139
                if (count < 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   140
                    throw new SocketException("Malformed reply from SOCKS server");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   141
                received += count;
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   142
            }
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   143
        } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   144
            setOption(SocketOptions.SO_TIMEOUT, originalTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   150
     * Provides the authentication mechanism required by the proxy.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private boolean authenticate(byte method, InputStream in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                 BufferedOutputStream out) throws IOException {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   154
        return authenticate(method, in, out, 0L);
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   155
    }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   156
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   157
    private boolean authenticate(byte method, InputStream in,
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   158
                                 BufferedOutputStream out,
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   159
                                 long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // No Authentication required. We're done then!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (method == NO_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return true;
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   163
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * User/Password authentication. Try, in that order :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * - The application provided Authenticator, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * - the user.name & no password (backward compatibility behavior).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (method == USER_PASSW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            String userName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            String password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            final InetAddress addr = InetAddress.getByName(server);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   172
            PasswordAuthentication pw =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   174
                    new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   175
                        public PasswordAuthentication run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                return Authenticator.requestPasswordAuthentication(
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   177
                                       server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                userName = pw.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                password = new String(pw.getPassword());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            } else {
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
   184
                userName = StaticProperty.userName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (userName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            out.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            out.write(userName.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                out.write(userName.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                out.write(password.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    out.write(password.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            out.flush();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   205
            byte[] data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   206
            int i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (i != 2 || data[1] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                /* RFC 1929 specifies that the connection MUST be closed if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                   authentication fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            /* Authentication succeeded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * GSSAPI authentication mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * Unfortunately the RFC seems out of sync with the Reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * implementation. I'll leave this in for future completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
//      if (method == GSSAPI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
//          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
//              GSSManager manager = GSSManager.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
//              GSSName name = manager.createName("SERVICE:socks@"+server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
//                                                   null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
//              GSSContext context = manager.createContext(name, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
//                                                         GSSContext.DEFAULT_LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
//              context.requestMutualAuth(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
//              context.requestReplayDet(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
//              context.requestSequenceDet(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
//              context.requestCredDeleg(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
//              byte []inToken = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
//              while (!context.isEstablished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
//                  byte[] outToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
//                      = context.initSecContext(inToken, 0, inToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
//                  // send the output token if generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
//                  if (outToken != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
//                      out.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
//                      out.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
//                      out.writeShort(outToken.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
//                      out.write(outToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
//                      out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
//                      data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   245
//                      i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
//                      if (i != 2 || data[1] == 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
//                          in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
//                          out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
//                          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
//                      }
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   251
//                      i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
//                      int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
//                      len = ((int)data[0] & 0xff) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
//                      len += data[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
//                      data = new byte[len];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   256
//                      i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
//                      if (i == len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
//                          return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
//                      in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
//                      out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
//                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
//              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
//          } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
//              /* RFC 1961 states that if Context initialisation fails the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
//                 MUST be closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
//              e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
//              in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
//              out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
//          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
//      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private void connectV4(InputStream in, OutputStream out,
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   275
                           InetSocketAddress endpoint,
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   276
                           long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (!(endpoint.getAddress() instanceof Inet4Address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new SocketException("SOCKS V4 requires IPv4 only addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        out.write(CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        out.write((endpoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        out.write((endpoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        out.write(endpoint.getAddress().getAddress());
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
   285
        String userName = getUserName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            out.write(userName.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        byte[] data = new byte[8];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   294
        int n = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (n != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new SocketException("Reply from SOCKS server has bad length: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (data[0] != 0 && data[0] != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new SocketException("Reply from SOCKS server has bad version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        SocketException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        case 90:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            external_address = endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        case 91:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            ex = new SocketException("SOCKS request rejected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        case 92:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            ex = new SocketException("SOCKS server couldn't reach destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        case 93:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            ex = new SocketException("SOCKS authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ex = new SocketException("Reply from SOCKS server contains bad status");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Connects the Socks Socket to the specified endpoint. It will first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * connect to the SOCKS proxy and negotiate the access. If the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * grants the connections, then the connect is successful and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * further traffic will go to the "real" endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   331
     * @param   endpoint        the {@code SocketAddress} to connect to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param   timeout         the timeout value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  IOException     if the connection can't be established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws  SecurityException if there is a security manager and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *                          doesn't allow the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws  IllegalArgumentException if endpoint is null or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   339
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    protected void connect(SocketAddress endpoint, int timeout) throws IOException {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   341
        final long deadlineMillis;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   342
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   343
        if (timeout == 0) {
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   344
            deadlineMillis = 0L;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   345
        } else {
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   346
            long finish = System.currentTimeMillis() + timeout;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   347
            deadlineMillis = finish < 0 ? Long.MAX_VALUE : finish;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   348
        }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   349
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (endpoint == null || !(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                security.checkConnect(epoint.getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                security.checkConnect(epoint.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // This is the general case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // server is not null only when the socket was created with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // specified proxy in which case it does bypass the ProxySelector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   366
            ProxySelector sel = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   367
                new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   368
                    public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                            return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (sel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                 * No default proxySelector --> direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                 */
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   376
                super.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   379
            URI uri;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            String host = epoint.getHostString();
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   382
            // IPv6 literal?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (epoint.getAddress() instanceof Inet6Address &&
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22276
diff changeset
   384
                (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                uri = new URI("socket://" + ParseUtil.encodePath(host) + ":"+ epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // This shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                assert false : e;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   392
                uri = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            Proxy p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            IOException savedExc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            java.util.Iterator<Proxy> iProxy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            iProxy = sel.select(uri).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (iProxy == null || !(iProxy.hasNext())) {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   399
                super.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            while (iProxy.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                p = iProxy.next();
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   404
                if (p == null || p.type() != Proxy.Type.SOCKS) {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   405
                    super.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   408
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (!(p.address() instanceof InetSocketAddress))
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   410
                    throw new SocketException("Unknown address type for proxy: " + p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                server = ((InetSocketAddress) p.address()).getHostString();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   413
                serverPort = ((InetSocketAddress) p.address()).getPort();
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
   414
                useV4 = useV4(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                try {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   418
                    privilegedConnect(server, serverPort, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    // Worked, let's get outta here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    // Ooops, let's notify the ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    sel.connectFailed(uri,p.address(),e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    server = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   425
                    serverPort = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    savedExc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    // Will continue the while loop and try the next proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
             * If server is still null at this point, none of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
             * worked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                throw new SocketException("Can't connect to SOCKS proxy:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                          + savedExc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            try {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   442
                privilegedConnect(server, serverPort, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   448
        // cmdIn & cmdOut were initialized during the privilegedConnect() call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        InputStream in = cmdIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (useV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // SOCKS Protocol version 4 doesn't know how to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // DOMAIN type of addresses (unresolved addresses here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                throw new UnknownHostException(epoint.toString());
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   457
            connectV4(in, out, epoint, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // This is SOCKS V5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        out.write(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        out.write(NO_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        out.write(USER_PASSW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        byte[] data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   468
        int i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (i != 2 || ((int)data[0]) != PROTO_VERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // Maybe it's not a V5 sever after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // Let's try V4 before we give up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            // SOCKS Protocol version 4 doesn't know how to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // DOMAIN type of addresses (unresolved addresses here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                throw new UnknownHostException(epoint.toString());
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   476
            connectV4(in, out, epoint, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (((int)data[1]) == NO_METHODS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            throw new SocketException("SOCKS : No acceptable methods");
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   481
        if (!authenticate(data[1], in, out, deadlineMillis)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new SocketException("SOCKS : authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        out.write(CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /* Test for IPV4/IPV6/Unresolved */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (epoint.isUnresolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            out.write(DOMAIN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            out.write(epoint.getHostName().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                out.write(epoint.getHostName().getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else if (epoint.getAddress() instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            out.write(IPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            out.write(epoint.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            out.write(epoint.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        data = new byte[4];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   511
        i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (i != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw new SocketException("Reply from SOCKS server has bad length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        SocketException ex = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   515
        int len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        case REQUEST_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            switch(data[3]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                addr = new byte[4];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   523
                i = readSocksReply(in, addr, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if (i != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   527
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            case DOMAIN_NAME:
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   532
                byte[] lenByte = new byte[1];
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   533
                i = readSocksReply(in, lenByte, deadlineMillis);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   534
                if (i != 1)
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   535
                    throw new SocketException("Reply from SOCKS server badly formatted");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   536
                len = lenByte[0] & 0xFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                byte[] host = new byte[len];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   538
                i = readSocksReply(in, host, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   542
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            case IPV6:
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   547
                len = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                addr = new byte[len];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   549
                i = readSocksReply(in, addr, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                data = new byte[2];
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   553
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                ex = new SocketException("Reply from SOCKS server contains wrong code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        case GENERAL_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            ex = new SocketException("SOCKS server general failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        case NOT_ALLOWED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            ex = new SocketException("SOCKS: Connection not allowed by ruleset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        case NET_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            ex = new SocketException("SOCKS: Network unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        case HOST_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            ex = new SocketException("SOCKS: Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        case CONN_REFUSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            ex = new SocketException("SOCKS: Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        case TTL_EXPIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            ex =  new SocketException("SOCKS: TTL expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        case CMD_NOT_SUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            ex = new SocketException("SOCKS: Command not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        case ADDR_TYPE_NOT_SUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            ex = new SocketException("SOCKS: address type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        external_address = epoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private void bindV4(InputStream in, OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        InetAddress baddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        int lport) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (!(baddr instanceof Inet4Address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            throw new SocketException("SOCKS V4 requires IPv4 only addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        super.bind(baddr, lport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        byte[] addr1 = baddr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        /* Test for AnyLocal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        InetAddress naddr = baddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (naddr.isAnyLocalAddress()) {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   606
            naddr = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   607
                        new PrivilegedAction<>() {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   608
                            public InetAddress run() {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   609
                                return cmdsock.getLocalAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   610
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   611
                            }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   612
                        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            addr1 = naddr.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        out.write(BIND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        out.write((super.getLocalPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        out.write((super.getLocalPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        out.write(addr1);
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
   620
        String userName = getUserName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            out.write(userName.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        byte[] data = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        int n = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (n != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            throw new SocketException("Reply from SOCKS server has bad length: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (data[0] != 0 && data[0] != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new SocketException("Reply from SOCKS server has bad version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        SocketException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        case 90:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // Success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            external_address = new InetSocketAddress(baddr, lport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        case 91:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            ex = new SocketException("SOCKS request rejected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        case 92:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            ex = new SocketException("SOCKS server couldn't reach destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        case 93:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            ex = new SocketException("SOCKS authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            ex = new SocketException("Reply from SOCKS server contains bad status");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Sends the Bind request to the SOCKS proxy. In the SOCKS protocol, bind
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   663
     * means "accept incoming connection from", so the SocketAddress is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * the one of the host we do accept connection from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 7982
diff changeset
   666
     * @param      saddr   the Socket address of the remote host.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @exception  IOException  if an I/O error occurs when binding this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    protected synchronized void socksBind(InetSocketAddress saddr) throws IOException {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   670
        if (((SocketImpl) this).socket != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            // this is a client socket, not a server socket, don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // call the SOCKS proxy for a bind!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            // This is the general case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // server is not null only when the socket was created with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            // specified proxy in which case it does bypass the ProxySelector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   682
            ProxySelector sel = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   683
                new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   684
                    public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (sel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                 * No default proxySelector --> direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   694
            URI uri;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            String host = saddr.getHostString();
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   697
            // IPv6 literal?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (saddr.getAddress() instanceof Inet6Address &&
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22276
diff changeset
   699
                (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                uri = new URI("serversocket://" + ParseUtil.encodePath(host) + ":"+ saddr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                // This shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                assert false : e;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   707
                uri = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            Proxy p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            Exception savedExc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            java.util.Iterator<Proxy> iProxy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            iProxy = sel.select(uri).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            if (iProxy == null || !(iProxy.hasNext())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            while (iProxy.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                p = iProxy.next();
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   718
                if (p == null || p.type() != Proxy.Type.SOCKS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                }
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   721
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                if (!(p.address() instanceof InetSocketAddress))
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   723
                    throw new SocketException("Unknown address type for proxy: " + p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                server = ((InetSocketAddress) p.address()).getHostString();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   726
                serverPort = ((InetSocketAddress) p.address()).getPort();
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
   727
                useV4 = useV4(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   731
                    AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   732
                        new PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   733
                            public Void run() throws Exception {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   734
                                cmdsock = new Socket(new NioSocketImpl(false));
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   735
                                cmdsock.connect(new InetSocketAddress(server, serverPort));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                cmdIn = cmdsock.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                cmdOut = cmdsock.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    // Ooops, let's notify the ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    server = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   745
                    serverPort = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    savedExc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    // Will continue the while loop and try the next proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             * If server is still null at this point, none of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
             * worked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (server == null || cmdsock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                throw new SocketException("Can't connect to SOCKS proxy:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                          + savedExc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   762
                AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   763
                    new PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   764
                        public Void run() throws Exception {
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   765
                            cmdsock = new Socket(new NioSocketImpl(false));
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   766
                            cmdsock.connect(new InetSocketAddress(server, serverPort));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                            cmdIn = cmdsock.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                            cmdOut = cmdsock.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        InputStream in = cmdIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        if (useV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            bindV4(in, out, saddr.getAddress(), saddr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        out.write(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        out.write(NO_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        out.write(USER_PASSW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        byte[] data = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        int i = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (i != 2 || ((int)data[0]) != PROTO_VERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            // Maybe it's not a V5 sever after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            // Let's try V4 before we give up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            bindV4(in, out, saddr.getAddress(), saddr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (((int)data[1]) == NO_METHODS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            throw new SocketException("SOCKS : No acceptable methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (!authenticate(data[1], in, out)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            throw new SocketException("SOCKS : authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // We're OK. Let's issue the BIND command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        out.write(BIND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        int lport = saddr.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        if (saddr.isUnresolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            out.write(DOMAIN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            out.write(saddr.getHostName().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                out.write(saddr.getHostName().getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            out.write((lport >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            out.write((lport >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        } else if (saddr.getAddress() instanceof Inet4Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            byte[] addr1 = saddr.getAddress().getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            out.write(addr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            out.write((lport >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            out.write((lport >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        } else if (saddr.getAddress() instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            byte[] addr1 = saddr.getAddress().getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            out.write(IPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            out.write(addr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            out.write((lport >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            out.write((lport >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            cmdsock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            throw new SocketException("unsupported address type : " + saddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        data = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        i = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        SocketException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        int len, nport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        case REQUEST_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            switch(data[3]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                addr = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                i = readSocksReply(in, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                if (i != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                data = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                i = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                nport = ((int)data[0] & 0xff) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                nport += ((int)data[1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                external_address =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    new InetSocketAddress(new Inet4Address("", addr) , nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            case DOMAIN_NAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                len = data[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                byte[] host = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                i = readSocksReply(in, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                data = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                i = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                nport = ((int)data[0] & 0xff) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                nport += ((int)data[1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                external_address = new InetSocketAddress(new String(host), nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            case IPV6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                len = data[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                addr = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                i = readSocksReply(in, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                data = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                i = readSocksReply(in, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                nport = ((int)data[0] & 0xff) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                nport += ((int)data[1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                external_address =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    new InetSocketAddress(new Inet6Address("", addr), nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        case GENERAL_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            ex = new SocketException("SOCKS server general failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        case NOT_ALLOWED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            ex = new SocketException("SOCKS: Bind not allowed by ruleset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        case NET_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            ex = new SocketException("SOCKS: Network unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        case HOST_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            ex = new SocketException("SOCKS: Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        case CONN_REFUSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            ex = new SocketException("SOCKS: Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        case TTL_EXPIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            ex =  new SocketException("SOCKS: TTL expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        case CMD_NOT_SUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            ex = new SocketException("SOCKS: Command not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        case ADDR_TYPE_NOT_SUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            ex = new SocketException("SOCKS: address type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            cmdsock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        cmdIn = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        cmdOut = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Accepts a connection from a specific host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param      s   the accepted connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param      saddr the socket address of the host we do accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *               connection from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @exception  IOException  if an I/O error occurs when accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    protected void acceptFrom(SocketImpl s, InetSocketAddress saddr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        if (cmdsock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // Not a Socks ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        InputStream in = cmdIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // Sends the "SOCKS BIND" request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        socksBind(saddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        int i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        SocketException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        int nport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        InetSocketAddress real_end = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        case REQUEST_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            i = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            switch(i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                addr = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                readSocksReply(in, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                nport = in.read() << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                nport += in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                real_end =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    new InetSocketAddress(new Inet4Address("", addr) , nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            case DOMAIN_NAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                int len = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                addr = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                readSocksReply(in, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                nport = in.read() << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                nport += in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                real_end = new InetSocketAddress(new String(addr), nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            case IPV6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                addr = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                readSocksReply(in, addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                nport = in.read() << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                nport += in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                real_end =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    new InetSocketAddress(new Inet6Address("", addr), nport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        case GENERAL_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            ex = new SocketException("SOCKS server general failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        case NOT_ALLOWED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            ex = new SocketException("SOCKS: Accept not allowed by ruleset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        case NET_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            ex = new SocketException("SOCKS: Network unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        case HOST_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            ex = new SocketException("SOCKS: Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        case CONN_REFUSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            ex = new SocketException("SOCKS: Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        case TTL_EXPIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            ex =  new SocketException("SOCKS: TTL expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        case CMD_NOT_SUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            ex = new SocketException("SOCKS: Command not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        case ADDR_TYPE_NOT_SUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            ex = new SocketException("SOCKS: address type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            cmdIn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            cmdOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            cmdsock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         * This is where we have to do some fancy stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * The datastream from the socket "accepted" by the proxy will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * come through the cmdSocket. So we have to swap the socketImpls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        if (s instanceof SocksSocketImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            ((SocksSocketImpl)s).external_address = real_end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (s instanceof PlainSocketImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            PlainSocketImpl psi = (PlainSocketImpl) s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            psi.setInputStream((SocketInputStream) in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            psi.setFileDescriptor(cmdsock.getImpl().getFileDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            psi.setAddress(cmdsock.getImpl().getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            psi.setPort(cmdsock.getImpl().getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            psi.setLocalPort(cmdsock.getImpl().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            s.fd = cmdsock.getImpl().fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            s.address = cmdsock.getImpl().address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            s.port = cmdsock.getImpl().port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            s.localport = cmdsock.getImpl().localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // Need to do that so that the socket won't be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        // when the ServerSocket is closed by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // It kinds of detaches the Socket because it is now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // used elsewhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
  1042
     * Returns the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
  1044
     * @return  the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
  1047
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    protected InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (external_address != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return external_address.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            return super.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
  1056
     * Returns the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
  1058
     * @return  the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @see     java.net.SocketImpl#port
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
  1061
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    protected int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if (external_address != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return external_address.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return super.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
  1069
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    protected void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        if (cmdsock != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            cmdsock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1077
    private String getUserName() {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1078
        String userName = "";
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1079
        if (applicationSetProxy) {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1080
            try {
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1081
                userName = System.getProperty("user.name");
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1082
            } catch (SecurityException se) { /* swallow Exception */ }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1083
        } else {
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
  1084
            userName = StaticProperty.userName();
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1085
        }
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1086
        return userName;
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
  1087
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
}