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