src/java.base/share/classes/java/net/SocksSocketImpl.java
author alanb
Tue, 26 Feb 2019 11:14:51 +0000
branchniosocketimpl-branch
changeset 57212 28b0946d3b81
parent 57172 63ab5af5d009
child 57268 adcdd45830a0
permissions -rw-r--r--
Cleanup/comments
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;
57112
alanb
parents: 57110 53473
diff changeset
    26
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
    32
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
    33
import jdk.internal.util.StaticProperty;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents: 7668
diff changeset
    34
import sun.net.SocksProxy;
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    35
import sun.net.spi.DefaultProxySelector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
    42
class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String server = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
    44
    private int serverPort = DEFAULT_PORT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private InetSocketAddress external_address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean useV4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private Socket cmdsock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private InputStream cmdIn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private OutputStream cmdOut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
    51
    SocksSocketImpl(SocketImpl delegate) {
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
    52
        super(delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
    55
    SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
    56
        super(delegate);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        SocketAddress a = proxy.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (a instanceof InetSocketAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            InetSocketAddress ad = (InetSocketAddress) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            server = ad.getHostString();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
    62
            serverPort = ad.getPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    64
        useV4 = useV4(proxy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    67
    private static boolean useV4(Proxy proxy) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    68
        if (proxy instanceof SocksProxy
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    69
            && ((SocksProxy)proxy).protocolVersion() == 4) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    70
            return true;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    71
        }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    72
        return DefaultProxySelector.socksProxyVersion() == 4;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    73
    }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private synchronized void privilegedConnect(final String host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                              final int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                              final int timeout)
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
    78
        throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
    82
                new java.security.PrivilegedExceptionAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    83
                    public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                              superConnectServer(host, port, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                              cmdIn = getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                              cmdOut = getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                              return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                      });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw (IOException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void superConnectServer(String host, int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                    int timeout) throws IOException {
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
    97
        delegate.connect(new InetSocketAddress(host, port), timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   100
    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
   101
        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
   102
            return 0;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   103
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   104
        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
   105
        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
   106
            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
   107
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   108
        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
   109
    }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   110
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   111
    private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int len = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int received = 0;
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   114
        int originalTimeout = (int) getOption(SocketOptions.SO_TIMEOUT);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   115
        try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   116
            while (received < len) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   117
                int count;
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   118
                int remaining = remainingMillis(deadlineMillis);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   119
                setOption(SocketOptions.SO_TIMEOUT, remaining);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   120
                try {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   121
                    count = in.read(data, received, len - received);
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   122
                } catch (SocketTimeoutException e) {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   123
                    throw new SocketTimeoutException("Connect timed out");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   124
                }
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   125
                if (count < 0)
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   126
                    throw new SocketException("Malformed reply from SOCKS server");
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   127
                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
   128
            }
57110
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   129
        } finally {
b848ca1ef778 Prototype of NIO based SocketImpl
alanb
parents: 52499
diff changeset
   130
            setOption(SocketOptions.SO_TIMEOUT, originalTimeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   135
    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
   136
                                 BufferedOutputStream out,
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   137
                                 long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // No Authentication required. We're done then!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (method == NO_AUTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return true;
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   141
        /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * User/Password authentication. Try, in that order :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * - The application provided Authenticator, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * - the user.name & no password (backward compatibility behavior).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (method == USER_PASSW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            String userName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            String password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            final InetAddress addr = InetAddress.getByName(server);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   150
            PasswordAuthentication pw =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   152
                    new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   153
                        public PasswordAuthentication run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                return Authenticator.requestPasswordAuthentication(
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   155
                                       server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                userName = pw.getUserName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                password = new String(pw.getPassword());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 47478
diff changeset
   162
                userName = StaticProperty.userName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (userName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.write(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out.write(userName.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                out.write(userName.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                out.write(password.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    out.write(password.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            out.flush();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   183
            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
   184
            int i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (i != 2 || data[1] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                /* RFC 1929 specifies that the connection MUST be closed if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                   authentication fails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            /* Authentication succeeded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    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
   199
                           InetSocketAddress endpoint,
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   200
                           long deadlineMillis) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (!(endpoint.getAddress() instanceof Inet4Address)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new SocketException("SOCKS V4 requires IPv4 only addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        out.write(PROTO_VERS4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        out.write(CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        out.write((endpoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        out.write((endpoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        out.write(endpoint.getAddress().getAddress());
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
   209
        String userName = getUserName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            out.write(userName.getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        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
   218
        int n = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (n != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throw new SocketException("Reply from SOCKS server has bad length: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (data[0] != 0 && data[0] != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new SocketException("Reply from SOCKS server has bad version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        SocketException ex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        case 90:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // Success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            external_address = endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        case 91:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            ex = new SocketException("SOCKS request rejected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        case 92:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            ex = new SocketException("SOCKS server couldn't reach destination");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        case 93:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            ex = new SocketException("SOCKS authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            ex = new SocketException("Reply from SOCKS server contains bad status");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   249
    @Override
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   250
    protected void connect(String host, int port) throws IOException {
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   251
        connect(new InetSocketAddress(host, port), 0);
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   252
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   253
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   254
    @Override
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   255
    protected void connect(InetAddress address, int port) throws IOException {
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   256
        connect(new InetSocketAddress(address, port), 0);
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   257
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   258
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   259
    @Override
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   260
    void setSocket(Socket soc) {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   261
        delegate.socket = soc;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   262
        super.setSocket(soc);
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   263
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   264
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   265
    @Override
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   266
    void setServerSocket(ServerSocket soc) {
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   267
        throw new InternalError("should not get here");
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   268
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Connects the Socks Socket to the specified endpoint. It will first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * connect to the SOCKS proxy and negotiate the access. If the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * grants the connections, then the connect is successful and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * further traffic will go to the "real" endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   276
     * @param   endpoint        the {@code SocketAddress} to connect to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param   timeout         the timeout value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws  IOException     if the connection can't be established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws  SecurityException if there is a security manager and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *                          doesn't allow the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws  IllegalArgumentException if endpoint is null or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *          SocketAddress subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   284
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    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
   286
        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
   287
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   288
        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
   289
            deadlineMillis = 0L;
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   290
        } else {
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   291
            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
   292
            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
   293
        }
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (endpoint == null || !(endpoint instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        InetSocketAddress epoint = (InetSocketAddress) endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                security.checkConnect(epoint.getHostName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                security.checkConnect(epoint.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                      epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // This is the general case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // server is not null only when the socket was created with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // specified proxy in which case it does bypass the ProxySelector
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   311
            ProxySelector sel = java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29112
diff changeset
   312
                new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   313
                    public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                            return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (sel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                 * No default proxySelector --> direct connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                 */
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   321
                delegate.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   324
            URI uri;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            String host = epoint.getHostString();
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50817
diff changeset
   327
            // IPv6 literal?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (epoint.getAddress() instanceof Inet6Address &&
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22276
diff changeset
   329
                (!host.startsWith("[")) && (host.indexOf(':') >= 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                uri = new URI("socket://" + ParseUtil.encodePath(host) + ":"+ epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // This shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                assert false : e;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   337
                uri = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            Proxy p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            IOException savedExc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            java.util.Iterator<Proxy> iProxy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            iProxy = sel.select(uri).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (iProxy == null || !(iProxy.hasNext())) {
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   344
                delegate.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            while (iProxy.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                p = iProxy.next();
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   349
                if (p == null || p.type() != Proxy.Type.SOCKS) {
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   350
                    delegate.connect(epoint, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   353
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (!(p.address() instanceof InetSocketAddress))
29112
df7e4edb6566 7178362: Socket impls should ignore unsupported proxy types rather than throwing
coffeys
parents: 25859
diff changeset
   355
                    throw new SocketException("Unknown address type for proxy: " + p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // Use getHostString() to avoid reverse lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                server = ((InetSocketAddress) p.address()).getHostString();
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   358
                serverPort = ((InetSocketAddress) p.address()).getPort();
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 29986
diff changeset
   359
                useV4 = useV4(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                try {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   363
                    privilegedConnect(server, serverPort, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    // Worked, let's get outta here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    // Ooops, let's notify the ProxySelector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    sel.connectFailed(uri,p.address(),e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    server = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   370
                    serverPort = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    savedExc = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // Will continue the while loop and try the next proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
             * If server is still null at this point, none of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
             * worked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                throw new SocketException("Can't connect to SOCKS proxy:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                          + savedExc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            // Connects to the SOCKS server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            try {
5147
96642e83ad41 6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents: 3464
diff changeset
   387
                privilegedConnect(server, serverPort, remainingMillis(deadlineMillis));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19069
diff changeset
   393
        // cmdIn & cmdOut were initialized during the privilegedConnect() call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        BufferedOutputStream out = new BufferedOutputStream(cmdOut, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        InputStream in = cmdIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (useV4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // SOCKS Protocol version 4 doesn't know how to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            // DOMAIN type of addresses (unresolved addresses here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                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
   402
            connectV4(in, out, epoint, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // This is SOCKS V5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        out.write(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        out.write(NO_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        out.write(USER_PASSW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        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
   413
        int i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (i != 2 || ((int)data[0]) != PROTO_VERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // Maybe it's not a V5 sever after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // Let's try V4 before we give up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // SOCKS Protocol version 4 doesn't know how to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // DOMAIN type of addresses (unresolved addresses here)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                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
   421
            connectV4(in, out, epoint, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (((int)data[1]) == NO_METHODS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            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
   426
        if (!authenticate(data[1], in, out, deadlineMillis)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new SocketException("SOCKS : authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        out.write(PROTO_VERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        out.write(CONNECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        out.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        /* Test for IPV4/IPV6/Unresolved */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (epoint.isUnresolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            out.write(DOMAIN_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            out.write(epoint.getHostName().length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                out.write(epoint.getHostName().getBytes("ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } catch (java.io.UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } else if (epoint.getAddress() instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            out.write(IPV6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            out.write(epoint.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            out.write(IPV4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            out.write(epoint.getAddress().getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            out.write((epoint.getPort() >> 8) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            out.write((epoint.getPort() >> 0) & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        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
   456
        i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (i != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            throw new SocketException("Reply from SOCKS server has bad length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        SocketException ex = null;
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   460
        int len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        byte[] addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        switch (data[1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        case REQUEST_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            // success!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            switch(data[3]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            case IPV4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                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
   468
                i = readSocksReply(in, addr, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                if (i != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                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
   472
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            case DOMAIN_NAME:
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   477
                byte[] lenByte = new byte[1];
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   478
                i = readSocksReply(in, lenByte, deadlineMillis);
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   479
                if (i != 1)
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   480
                    throw new SocketException("Reply from SOCKS server badly formatted");
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   481
                len = lenByte[0] & 0xFF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                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
   483
                i = readSocksReply(in, host, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                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
   487
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            case IPV6:
22276
7fc4c8b08e49 7100957: SOCKS proxying does not work with IPv6 connections
chegar
parents: 21278
diff changeset
   492
                len = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                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
   494
                i = readSocksReply(in, addr, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                if (i != len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                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
   498
                i = readSocksReply(in, data, deadlineMillis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (i != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    throw new SocketException("Reply from SOCKS server badly formatted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                ex = new SocketException("Reply from SOCKS server contains wrong code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        case GENERAL_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            ex = new SocketException("SOCKS server general failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        case NOT_ALLOWED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            ex = new SocketException("SOCKS: Connection not allowed by ruleset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        case NET_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            ex = new SocketException("SOCKS: Network unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        case HOST_UNREACHABLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            ex = new SocketException("SOCKS: Host unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        case CONN_REFUSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            ex = new SocketException("SOCKS: Connection refused");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        case TTL_EXPIRED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            ex =  new SocketException("SOCKS: TTL expired");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        case CMD_NOT_SUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            ex = new SocketException("SOCKS: Command not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        case ADDR_TYPE_NOT_SUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            ex = new SocketException("SOCKS: address type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        external_address = epoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   540
    @Override
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   541
    protected void listen(int backlog) {
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   542
        throw new InternalError("should not get here");
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   543
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   545
    @Override
57172
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   546
    protected void accept(SocketImpl s) {
63ab5af5d009 Moving delegating SocketImpl to its own class
alanb
parents: 57171
diff changeset
   547
        throw new InternalError("should not get here");
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   548
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   551
     * Returns the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   553
     * @return  the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   556
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    protected InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (external_address != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return external_address.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        else
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   561
            return delegate.getInetAddress();
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   562
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   563
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   565
     * Returns the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 18212
diff changeset
   567
     * @return  the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @see     java.net.SocketImpl#port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
3051
9481bd560a57 6852108: Remove Preferences dependance from SocksSocketImpl
jccollet
parents: 715
diff changeset
   570
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    protected int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (external_address != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return external_address.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        else
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   575
            return delegate.getPort();
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   576
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   577
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   578
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    protected void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (cmdsock != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            cmdsock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        cmdsock = null;
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   583
        delegate.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
   586
    private String getUserName() {
53473
9366628d727b 8216986: Remove unused code from SocksSocketImpl
michaelm
parents: 52499
diff changeset
   587
        return StaticProperty.userName();
3450
2f08a8bb9b83 6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents: 715
diff changeset
   588
    }
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   589
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   590
    @Override
57212
28b0946d3b81 Cleanup/comments
alanb
parents: 57172
diff changeset
   591
    void reset() {
28b0946d3b81 Cleanup/comments
alanb
parents: 57172
diff changeset
   592
        throw new InternalError("should not get here");
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 57112
diff changeset
   593
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
}