java.net
Class SocksSocketImpl

java.lang.Object
  extended byjava.net.SocketImpl
      extended byjava.net.PlainSocketImpl
          extended byjava.net.SocksSocketImpl
All Implemented Interfaces:
SocketOptions, SocksConsts

class SocksSocketImpl
extends PlainSocketImpl
implements SocksConsts

SOCKS (V4 & V5) TCP socket implementation (RFC 1928). This is a subclass of PlainSocketImpl. Note this class should NOT be public.


Field Summary
static int ADDR_TYPE_NOT_SUP
           
static int BIND
           
static int CMD_NOT_SUPPORTED
           
private  InputStream cmdIn
           
private  OutputStream cmdOut
           
private  Socket cmdsock
           
static int CONN_REFUSED
           
static int CONNECT
           
static int DEFAULT_PORT
           
static int DOMAIN_NAME
           
private  InetSocketAddress external_address
           
static int GENERAL_FAILURE
           
static int GSSAPI
           
static int HOST_UNREACHABLE
           
static int IPV4
           
static int IPV6
           
static int NET_UNREACHABLE
           
static int NO_AUTH
           
static int NO_METHODS
           
static int NOT_ALLOWED
           
private  int port
           
static int PROTO_VERS
           
static int PROTO_VERS4
           
static int REQUEST_OK
           
private  String server
           
static int SHUT_RD
           
static int SHUT_WR
           
(package private)  int timeout
           
static int TTL_EXPIRED
           
static int UDP_ASSOC
           
static int USER_PASSW
           
private  boolean useV4
           
 
Fields inherited from class java.net.SocketImpl
address, fd, localport, serverSocket, socket
 
Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
(package private) SocksSocketImpl(String server, int port)
           
 
Method Summary
protected  void accept(SocketImpl s)
          Accepts a connection.
 FileDescriptor acquireFD()
           
private  boolean authenticate(byte method, InputStream in, DataOutputStream out)
          Provides the authentication machanism required by the proxy.
protected  int available()
          Returns the number of bytes that can be read without blocking.
protected  void bind(InetAddress baddr, int lport)
          Binds this socket to the specified port number on the specified host.
private  void bindV4(InputStream in, OutputStream out, InetAddress baddr, int lport)
           
protected  void close()
          Closes the socket.
protected  void connect(InetAddress address, int port)
          Creates a socket and connects it to the specified address on the specified port.
protected  void connect(SocketAddress endpoint, int timeout)
          Connects the Socks Socket to the specified endpoint.
protected  void connect(String host, int port)
          Creates a socket and connects it to the specified port on the specified host.
private  void connectV4(InputStream in, OutputStream out, InetSocketAddress endpoint)
           
protected  void create(boolean stream)
          Creates a socket with a boolean that specifies whether this is a stream socket (true) or an unconnected UDP socket (false).
protected  void finalize()
          Cleans up if the user forgets to close it.
protected  InetAddress getInetAddress()
          Returns the value of this socket's address field.
protected  InputStream getInputStream()
          Gets an InputStream for this socket.
protected  int getLocalPort()
          Returns the value of this socket's localport field.
 Object getOption(int opt)
          Fetch the value of an option.
protected  OutputStream getOutputStream()
          Gets an OutputStream for this socket.
protected  int getPort()
          Returns the value of this socket's port field.
 int getTimeout()
           
 boolean isClosedOrPending()
           
 boolean isConnectionReset()
           
 boolean isConnectionResetPending()
           
protected  void listen(int count)
          Listens, for a specified amount of time, for connections.
private  void privilegedConnect(String host, int port, int timeout)
           
private  int readSocksReply(InputStream in, byte[] data)
           
 void releaseFD()
           
protected  void sendUrgentData(int data)
          Send one byte of urgent data on the socket.
 void setConnectionReset()
           
 void setConnectionResetPending()
           
(package private)  void setInputStream(SocketInputStream in)
           
 void setOption(int opt, Object val)
          Enable/disable the option specified by optID.
(package private)  void setV4()
           
protected  void shutdownInput()
          Shutdown read-half of the socket connection;
protected  void shutdownOutput()
          Shutdown write-half of the socket connection;
private  void superConnectServer(String host, int port, int timeout)
           
protected  boolean supportsUrgentData()
          Returns whether or not this SocketImpl supports sending urgent data.
 
Methods inherited from class java.net.SocketImpl
getFileDescriptor, getServerSocket, getSocket, reset, setServerSocket, setSocket, toString
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

server

private String server

port

private int port

external_address

private InetSocketAddress external_address

useV4

private boolean useV4

cmdsock

private Socket cmdsock

cmdIn

private InputStream cmdIn

cmdOut

private OutputStream cmdOut

PROTO_VERS4

public static final int PROTO_VERS4
See Also:
Constant Field Values

PROTO_VERS

public static final int PROTO_VERS
See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
See Also:
Constant Field Values

NO_AUTH

public static final int NO_AUTH
See Also:
Constant Field Values

GSSAPI

public static final int GSSAPI
See Also:
Constant Field Values

USER_PASSW

public static final int USER_PASSW
See Also:
Constant Field Values

NO_METHODS

public static final int NO_METHODS
See Also:
Constant Field Values

CONNECT

public static final int CONNECT
See Also:
Constant Field Values

BIND

public static final int BIND
See Also:
Constant Field Values

UDP_ASSOC

public static final int UDP_ASSOC
See Also:
Constant Field Values

IPV4

public static final int IPV4
See Also:
Constant Field Values

DOMAIN_NAME

public static final int DOMAIN_NAME
See Also:
Constant Field Values

IPV6

public static final int IPV6
See Also:
Constant Field Values

REQUEST_OK

public static final int REQUEST_OK
See Also:
Constant Field Values

GENERAL_FAILURE

public static final int GENERAL_FAILURE
See Also:
Constant Field Values

NOT_ALLOWED

public static final int NOT_ALLOWED
See Also:
Constant Field Values

NET_UNREACHABLE

public static final int NET_UNREACHABLE
See Also:
Constant Field Values

HOST_UNREACHABLE

public static final int HOST_UNREACHABLE
See Also:
Constant Field Values

CONN_REFUSED

public static final int CONN_REFUSED
See Also:
Constant Field Values

TTL_EXPIRED

public static final int TTL_EXPIRED
See Also:
Constant Field Values

CMD_NOT_SUPPORTED

public static final int CMD_NOT_SUPPORTED
See Also:
Constant Field Values

ADDR_TYPE_NOT_SUP

public static final int ADDR_TYPE_NOT_SUP
See Also:
Constant Field Values

timeout

int timeout

SHUT_RD

public static final int SHUT_RD
See Also:
Constant Field Values

SHUT_WR

public static final int SHUT_WR
See Also:
Constant Field Values
Constructor Detail

SocksSocketImpl

SocksSocketImpl(String server,
                int port)
Method Detail

setV4

void setV4()

privilegedConnect

private void privilegedConnect(String host,
                               int port,
                               int timeout)
                        throws IOException
Throws:
IOException

superConnectServer

private void superConnectServer(String host,
                                int port,
                                int timeout)
                         throws IOException
Throws:
IOException

readSocksReply

private int readSocksReply(InputStream in,
                           byte[] data)
                    throws IOException
Throws:
IOException

authenticate

private boolean authenticate(byte method,
                             InputStream in,
                             DataOutputStream out)
                      throws IOException
Provides the authentication machanism required by the proxy.

Throws:
IOException

connectV4

private void connectV4(InputStream in,
                       OutputStream out,
                       InetSocketAddress endpoint)
                throws IOException
Throws:
IOException

connect

protected void connect(SocketAddress endpoint,
                       int timeout)
                throws IOException
Connects the Socks Socket to the specified endpoint. It will first connect to the SOCKS proxy and negotiate the access. If the proxy grants the connections, then the connect is successful and all further traffic will go to the "real" endpoint.

Overrides:
connect in class PlainSocketImpl
Parameters:
endpoint - the SocketAddress to connect to.
timeout - the timeout value in milliseconds
Throws:
IOException - if the connection can't be established.
SecurityException - if there is a security manager and it doesn't allow the connection
IllegalArgumentException - if endpoint is null or a SocketAddress subclass not supported by this socket

bindV4

private void bindV4(InputStream in,
                    OutputStream out,
                    InetAddress baddr,
                    int lport)
             throws IOException
Throws:
IOException

bind

protected void bind(InetAddress baddr,
                    int lport)
             throws IOException
Binds this socket to the specified port number on the specified host.

Overrides:
bind in class PlainSocketImpl
Parameters:
baddr - the IP address of the remote host.
lport - the port number.
Throws:
IOException - if an I/O error occurs when binding this socket.

accept

protected void accept(SocketImpl s)
               throws IOException
Accepts a connection.

Overrides:
accept in class PlainSocketImpl
Parameters:
s - the accepted connection.
Throws:
IOException - if an I/O error occurs when accepting the connection.

getInetAddress

protected InetAddress getInetAddress()
Returns the value of this socket's address field.

Overrides:
getInetAddress in class SocketImpl
Returns:
the value of this socket's address field.
See Also:
SocketImpl.address

getPort

protected int getPort()
Returns the value of this socket's port field.

Overrides:
getPort in class SocketImpl
Returns:
the value of this socket's port field.
See Also:
SocketImpl.port

getLocalPort

protected int getLocalPort()
Description copied from class: SocketImpl
Returns the value of this socket's localport field.

Overrides:
getLocalPort in class SocketImpl
Returns:
the value of this socket's localport field.
See Also:
SocketImpl.localport

close

protected void close()
              throws IOException
Description copied from class: PlainSocketImpl
Closes the socket.

Overrides:
close in class PlainSocketImpl
Throws:
IOException

create

protected void create(boolean stream)
               throws IOException
Creates a socket with a boolean that specifies whether this is a stream socket (true) or an unconnected UDP socket (false).

Specified by:
create in class SocketImpl
Parameters:
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws:
IOException - if an I/O error occurs while creating the socket.

connect

protected void connect(String host,
                       int port)
                throws UnknownHostException,
                       IOException
Creates a socket and connects it to the specified port on the specified host.

Specified by:
connect in class SocketImpl
Parameters:
host - the specified host
port - the specified port
Throws:
IOException - if an I/O error occurs when connecting to the remote host.
UnknownHostException

connect

protected void connect(InetAddress address,
                       int port)
                throws IOException
Creates a socket and connects it to the specified address on the specified port.

Specified by:
connect in class SocketImpl
Parameters:
address - the address
port - the specified port
Throws:
IOException - if an I/O error occurs when attempting a connection.

setOption

public void setOption(int opt,
                      Object val)
               throws SocketException
Description copied from interface: SocketOptions
Enable/disable the option specified by optID. If the option is to be enabled, and it takes an option-specific "value", this is passed in value. The actual type of value is option-specific, and it is an error to pass something that isn't of the expected type:
 SocketImpl s;
 ...
 s.setOption(SO_LINGER, new Integer(10));
    // OK - set SO_LINGER w/ timeout of 10 sec.
 s.setOption(SO_LINGER, new Double(10));
    // ERROR - expects java.lang.Integer
If the requested option is binary, it can be set using this method by a java.lang.Boolean:
 s.setOption(TCP_NODELAY, new Boolean(true));
    // OK - enables TCP_NODELAY, a binary option
 

Any option can be disabled using this method with a Boolean(false):
 s.setOption(TCP_NODELAY, new Boolean(false));
    // OK - disables TCP_NODELAY
 s.setOption(SO_LINGER, new Boolean(false));
    // OK - disables SO_LINGER
 

For an option that has a notion of on and off, and requires a non-boolean parameter, setting its value to anything other than Boolean(false) implicitly enables it.
Throws SocketException if the option is unrecognized, the socket is closed, or some low-level error occurred

Parameters:
opt - identifies the option
val - the parameter of the socket option
Throws:
SocketException - if the option is unrecognized, the socket is closed, or some low-level error occurred
See Also:
SocketOptions.getOption(int)

getOption

public Object getOption(int opt)
                 throws SocketException
Description copied from interface: SocketOptions
Fetch the value of an option. Binary options will return java.lang.Boolean(true) if enabled, java.lang.Boolean(false) if disabled, e.g.:
 SocketImpl s;
 ...
 Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
 if (noDelay.booleanValue()) {
     // true if TCP_NODELAY is enabled...
 ...
 }
 

For options that take a particular type as a parameter, getOption(int) will return the paramter's value, else it will return java.lang.Boolean(false):

 Object o = s.getOption(SO_LINGER);
 if (o instanceof Integer) {
     System.out.print("Linger time is " + ((Integer)o).intValue());
 } else {
   // the true type of o is java.lang.Boolean(false);
 }
 

Parameters:
opt - an int identifying the option to fetch
Returns:
the value of the option
Throws:
SocketException - if optID is unknown along the protocol stack (including the SocketImpl)
See Also:
SocketOptions.setOption(int, java.lang.Object)

listen

protected void listen(int count)
               throws IOException
Listens, for a specified amount of time, for connections.

Specified by:
listen in class SocketImpl
Parameters:
count - the amount of time to listen for connections
Throws:
IOException - if an I/O error occurs when creating the queue.

getInputStream

protected InputStream getInputStream()
                              throws IOException
Gets an InputStream for this socket.

Specified by:
getInputStream in class SocketImpl
Returns:
a stream for reading from this socket.
Throws:
IOException - if an I/O error occurs when creating the input stream.

setInputStream

void setInputStream(SocketInputStream in)

getOutputStream

protected OutputStream getOutputStream()
                                throws IOException
Gets an OutputStream for this socket.

Specified by:
getOutputStream in class SocketImpl
Returns:
an output stream for writing to this socket.
Throws:
IOException - if an I/O error occurs when creating the output stream.

available

protected int available()
                 throws IOException
Returns the number of bytes that can be read without blocking.

Specified by:
available in class SocketImpl
Returns:
the number of bytes that can be read from this socket without blocking.
Throws:
IOException - if an I/O error occurs when determining the number of bytes available.

shutdownInput

protected void shutdownInput()
                      throws IOException
Shutdown read-half of the socket connection;

Overrides:
shutdownInput in class SocketImpl
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
Socket.shutdownOutput(), Socket.close(), Socket.setSoLinger(boolean, int)

shutdownOutput

protected void shutdownOutput()
                       throws IOException
Shutdown write-half of the socket connection;

Overrides:
shutdownOutput in class SocketImpl
Throws:
IOException - if an I/O error occurs when shutting down this socket.
See Also:
Socket.shutdownInput(), Socket.close(), Socket.setSoLinger(boolean, int)

supportsUrgentData

protected boolean supportsUrgentData()
Description copied from class: SocketImpl
Returns whether or not this SocketImpl supports sending urgent data. By default, false is returned unless the method is overridden in a sub-class

Overrides:
supportsUrgentData in class SocketImpl
Returns:
true if urgent data supported
See Also:
SocketImpl.address

sendUrgentData

protected void sendUrgentData(int data)
                       throws IOException
Description copied from class: SocketImpl
Send one byte of urgent data on the socket. The byte to be sent is the low eight bits of the parameter

Specified by:
sendUrgentData in class SocketImpl
Parameters:
data - The byte of data to send
Throws:
IOException - if there is an error sending the data.

finalize

protected void finalize()
                 throws IOException
Cleans up if the user forgets to close it.

Overrides:
finalize in class Object
Throws:
IOException

acquireFD

public final FileDescriptor acquireFD()

releaseFD

public final void releaseFD()

isConnectionReset

public boolean isConnectionReset()

isConnectionResetPending

public boolean isConnectionResetPending()

setConnectionReset

public void setConnectionReset()

setConnectionResetPending

public void setConnectionResetPending()

isClosedOrPending

public boolean isClosedOrPending()

getTimeout

public int getTimeout()