changeset 1307:ed38f9e6ad9a

6852108: Remove Preferences dependance from SocksSocketImpl Summary: Removed Preferences API use and fixed a few findbugs gotchas Reviewed-by: alanb
author jccollet
date Fri, 19 Jun 2009 14:12:22 +0200
parents 37ed72fe7561
children 77367060d119
files src/share/classes/java/net/SocksSocketImpl.java
diffstat 1 files changed, 25 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/SocksSocketImpl.java	Fri Jun 19 18:03:27 2009 +0800
+++ b/src/share/classes/java/net/SocksSocketImpl.java	Fri Jun 19 14:12:22 2009 +0200
@@ -29,7 +29,6 @@
 import java.io.BufferedOutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedExceptionAction;
-import java.util.prefs.Preferences;
 import sun.net.www.ParseUtil;
 /* import org.ietf.jgss.*; */
 
@@ -41,7 +40,7 @@
 
 class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
     private String server = null;
-    private int port = DEFAULT_PORT;
+    private int serverPort = DEFAULT_PORT;
     private InetSocketAddress external_address;
     private boolean useV4 = false;
     private Socket cmdsock = null;
@@ -54,7 +53,7 @@
 
     SocksSocketImpl(String server, int port) {
         this.server = server;
-        this.port = (port == -1 ? DEFAULT_PORT : port);
+        this.serverPort = (port == -1 ? DEFAULT_PORT : port);
     }
 
     SocksSocketImpl(Proxy proxy) {
@@ -63,7 +62,7 @@
             InetSocketAddress ad = (InetSocketAddress) a;
             // Use getHostString() to avoid reverse lookups
             server = ad.getHostString();
-            port = ad.getPort();
+            serverPort = ad.getPort();
         }
     }
 
@@ -113,16 +112,12 @@
      */
     private boolean authenticate(byte method, InputStream in,
                                  BufferedOutputStream out) throws IOException {
-        byte[] data = null;
-        int i;
         // No Authentication required. We're done then!
         if (method == NO_AUTH)
             return true;
         /**
          * User/Password authentication. Try, in that order :
          * - The application provided Authenticator, if any
-         * - The user preferences java.net.socks.username &
-         *   java.net.socks.password
          * - the user.name & no password (backward compatibility behavior).
          */
         if (method == USER_PASSW) {
@@ -134,40 +129,15 @@
                     new java.security.PrivilegedAction<PasswordAuthentication>() {
                         public PasswordAuthentication run() {
                                 return Authenticator.requestPasswordAuthentication(
-                                       server, addr, port, "SOCKS5", "SOCKS authentication", null);
+                                       server, addr, serverPort, "SOCKS5", "SOCKS authentication", null);
                             }
                         });
             if (pw != null) {
                 userName = pw.getUserName();
                 password = new String(pw.getPassword());
             } else {
-                final Preferences prefs = Preferences.userRoot().node("/java/net/socks");
-                try {
-                    userName = AccessController.doPrivileged(
-                        new java.security.PrivilegedExceptionAction<String>() {
-                            public String run() throws IOException {
-                                return prefs.get("username", null);
-                            }
-                        });
-                } catch (java.security.PrivilegedActionException pae) {
-                    throw (IOException) pae.getException();
-                }
-
-                if (userName != null) {
-                    try {
-                        password = AccessController.doPrivileged(
-                            new java.security.PrivilegedExceptionAction<String>() {
-                                public String run() throws IOException {
-                                    return prefs.get("password", null);
-                                }
-                            });
-                    } catch (java.security.PrivilegedActionException pae) {
-                        throw (IOException) pae.getException();
-                    }
-                } else {
-                    userName = java.security.AccessController.doPrivileged(
+                userName = java.security.AccessController.doPrivileged(
                         new sun.security.action.GetPropertyAction("user.name"));
-                }
             }
             if (userName == null)
                 return false;
@@ -188,8 +158,8 @@
             } else
                 out.write(0);
             out.flush();
-            data = new byte[2];
-            i = readSocksReply(in, data);
+            byte[] data = new byte[2];
+            int i = readSocksReply(in, data);
             if (i != 2 || data[1] != 0) {
                 /* RFC 1929 specifies that the connection MUST be closed if
                    authentication fails */
@@ -322,6 +292,7 @@
      * @throws  IllegalArgumentException if endpoint is null or a
      *          SocketAddress subclass not supported by this socket
      */
+    @Override
     protected void connect(SocketAddress endpoint, int timeout) throws IOException {
         SecurityManager security = System.getSecurityManager();
         if (endpoint == null || !(endpoint instanceof InetSocketAddress))
@@ -352,7 +323,7 @@
                 super.connect(epoint, timeout);
                 return;
             }
-            URI uri = null;
+            URI uri;
             // Use getHostString() to avoid reverse lookups
             String host = epoint.getHostString();
             // IPv6 litteral?
@@ -365,6 +336,7 @@
             } catch (URISyntaxException e) {
                 // This shouldn't happen
                 assert false : e;
+                uri = null;
             }
             Proxy p = null;
             IOException savedExc = null;
@@ -386,18 +358,18 @@
                     throw new SocketException("Unknow address type for proxy: " + p);
                 // Use getHostString() to avoid reverse lookups
                 server = ((InetSocketAddress) p.address()).getHostString();
-                port = ((InetSocketAddress) p.address()).getPort();
+                serverPort = ((InetSocketAddress) p.address()).getPort();
 
                 // Connects to the SOCKS server
                 try {
-                    privilegedConnect(server, port, timeout);
+                    privilegedConnect(server, serverPort, timeout);
                     // Worked, let's get outta here
                     break;
                 } catch (IOException e) {
                     // Ooops, let's notify the ProxySelector
                     sel.connectFailed(uri,p.address(),e);
                     server = null;
-                    port = -1;
+                    serverPort = -1;
                     savedExc = e;
                     // Will continue the while loop and try the next proxy
                 }
@@ -414,7 +386,7 @@
         } else {
             // Connects to the SOCKS server
             try {
-                privilegedConnect(server, port, timeout);
+                privilegedConnect(server, serverPort, timeout);
             } catch (IOException e) {
                 throw new SocketException(e.getMessage());
             }
@@ -487,7 +459,7 @@
         if (i != 4)
             throw new SocketException("Reply from SOCKS server has bad length");
         SocketException ex = null;
-        int nport, len;
+        int len;
         byte[] addr;
         switch (data[1]) {
         case REQUEST_OK:
@@ -502,8 +474,6 @@
                 i = readSocksReply(in, data);
                 if (i != 2)
                     throw new SocketException("Reply from SOCKS server badly formatted");
-                nport = ((int)data[0] & 0xff) << 8;
-                nport += ((int)data[1] & 0xff);
                 break;
             case DOMAIN_NAME:
                 len = data[1];
@@ -515,8 +485,6 @@
                 i = readSocksReply(in, data);
                 if (i != 2)
                     throw new SocketException("Reply from SOCKS server badly formatted");
-                nport = ((int)data[0] & 0xff) << 8;
-                nport += ((int)data[1] & 0xff);
                 break;
             case IPV6:
                 len = data[1];
@@ -528,8 +496,6 @@
                 i = readSocksReply(in, data);
                 if (i != 2)
                     throw new SocketException("Reply from SOCKS server badly formatted");
-                nport = ((int)data[0] & 0xff) << 8;
-                nport += ((int)data[1] & 0xff);
                 break;
             default:
                 ex = new SocketException("Reply from SOCKS server contains wrong code");
@@ -663,7 +629,7 @@
                  */
                 return;
             }
-            URI uri = null;
+            URI uri;
             // Use getHostString() to avoid reverse lookups
             String host = saddr.getHostString();
             // IPv6 litteral?
@@ -676,6 +642,7 @@
             } catch (URISyntaxException e) {
                 // This shouldn't happen
                 assert false : e;
+                uri = null;
             }
             Proxy p = null;
             Exception savedExc = null;
@@ -695,7 +662,7 @@
                     throw new SocketException("Unknow address type for proxy: " + p);
                 // Use getHostString() to avoid reverse lookups
                 server = ((InetSocketAddress) p.address()).getHostString();
-                port = ((InetSocketAddress) p.address()).getPort();
+                serverPort = ((InetSocketAddress) p.address()).getPort();
 
                 // Connects to the SOCKS server
                 try {
@@ -703,7 +670,7 @@
                         new PrivilegedExceptionAction<Void>() {
                             public Void run() throws Exception {
                                 cmdsock = new Socket(new PlainSocketImpl());
-                                cmdsock.connect(new InetSocketAddress(server, port));
+                                cmdsock.connect(new InetSocketAddress(server, serverPort));
                                 cmdIn = cmdsock.getInputStream();
                                 cmdOut = cmdsock.getOutputStream();
                                 return null;
@@ -713,7 +680,7 @@
                     // Ooops, let's notify the ProxySelector
                     sel.connectFailed(uri,p.address(),new SocketException(e.getMessage()));
                     server = null;
-                    port = -1;
+                    serverPort = -1;
                     cmdsock = null;
                     savedExc = e;
                     // Will continue the while loop and try the next proxy
@@ -734,7 +701,7 @@
                     new PrivilegedExceptionAction<Void>() {
                         public Void run() throws Exception {
                             cmdsock = new Socket(new PlainSocketImpl());
-                            cmdsock.connect(new InetSocketAddress(server, port));
+                            cmdsock.connect(new InetSocketAddress(server, serverPort));
                             cmdIn = cmdsock.getInputStream();
                             cmdOut = cmdsock.getOutputStream();
                             return null;
@@ -809,7 +776,6 @@
         switch (data[1]) {
         case REQUEST_OK:
             // success!
-            InetSocketAddress real_end = null;
             switch(data[3]) {
             case IPV4:
                 addr = new byte[4];
@@ -1016,6 +982,7 @@
      * @return  the value of this socket's <code>address</code> field.
      * @see     java.net.SocketImpl#address
      */
+    @Override
     protected InetAddress getInetAddress() {
         if (external_address != null)
             return external_address.getAddress();
@@ -1029,6 +996,7 @@
      * @return  the value of this socket's <code>port</code> field.
      * @see     java.net.SocketImpl#port
      */
+    @Override
     protected int getPort() {
         if (external_address != null)
             return external_address.getPort();
@@ -1036,6 +1004,7 @@
             return super.getPort();
     }
 
+    @Override
     protected int getLocalPort() {
         if (socket != null)
             return super.getLocalPort();
@@ -1045,6 +1014,7 @@
             return super.getLocalPort();
     }
 
+    @Override
     protected void close() throws IOException {
         if (cmdsock != null)
             cmdsock.close();