FileDocCategorySizeDatePackage
Socks4Message.javaAPI DocAndroid 1.5 API7395Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.net

Socks4Message

public class Socks4Message extends Object

Fields Summary
static final int
COMMAND_CONNECT
static final int
COMMAND_BIND
static final int
RETURN_SUCCESS
static final int
RETURN_FAILURE
static final int
RETURN_CANNOT_CONNECT_TO_IDENTD
static final int
RETURN_DIFFERENT_USER_IDS
static final int
REPLY_LENGTH
static final int
INDEX_VERSION
private static final int
SOCKS_VERSION
private static final int
INDEX_COMMAND
private static final int
INDEX_PORT
private static final int
INDEX_IP
private static final int
INDEX_USER_ID
private static final int
BUFFER_LENGTH
private static final int
MAX_USER_ID_LENGTH
protected byte[]
buffer
Constructors Summary
public Socks4Message()


      
        super();
        buffer = new byte[BUFFER_LENGTH];
        setVersionNumber(SOCKS_VERSION);
    
Methods Summary
public byte[]getBytes()
Answer the message's byte buffer.

        return buffer;
    
public intgetCommandOrResult()
Get the request's command or result.

        return buffer[INDEX_COMMAND];
    
public java.lang.StringgetErrorString(int error)
Answer an error string corresponding to the given error value.

        switch (error) {
            case RETURN_FAILURE:
                return Msg.getString("K00cd"); //$NON-NLS-1$
            case RETURN_CANNOT_CONNECT_TO_IDENTD:
                return Msg.getString("K00ce"); //$NON-NLS-1$
            case RETURN_DIFFERENT_USER_IDS:
                return Msg.getString("K00cf"); //$NON-NLS-1$
            default:
                return Msg.getString("K00d0"); //$NON-NLS-1$
        }
    
public intgetIP()

        return getInt32(INDEX_IP);
    
private intgetInt16(int offset)
Get a 16 bit integer from the buffer at the offset given.

        return (((buffer[offset] & 0xFF) << 8) + (buffer[offset + 1] & 0xFF));
    
private intgetInt32(int offset)
Get a 32 bit integer from the buffer at the offset given.

        return ((buffer[offset + 3] & 0xFF)
                + ((buffer[offset + 2] & 0xFF) << 8)
                + ((buffer[offset + 1] & 0xFF) << 16) + ((buffer[offset + 0] & 0xFF) << 24));
    
public intgetLength()
Answer the total number of bytes used for the request. This method searches for the end of the user id, then searches for the end of the password and returns the final index as the requests length.

        int index = 0;

        // Look for the end of the user id.
        for (index = INDEX_USER_ID; buffer[index] != 0; index++) {
            /*
             * Finds the end of the user id by searching for the null
             * termination of the user id string.
             */
        }

        // Increment the index to include the NULL character in the length;
        index++;
        return index;
    
public intgetPort()
Answer the request's port number.

        return getInt16(INDEX_PORT);
    
private java.lang.StringgetString(int offset, int maxLength)
Get a String from the buffer at the offset given. The method reads until it encounters a null value or reaches the maxLength given.

        int index = offset;
        int lastIndex = index + maxLength;
        String result;

        while (index < lastIndex && (buffer[index] != 0)) {
            index++;
        }
        try {
            result = new String(buffer, offset, index - offset, "ISO8859_1"); //$NON-NLS-1$
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.toString());
        }
        return result;
    
public java.lang.StringgetUserId()
Answer the user id for authentication.

        return getString(INDEX_USER_ID, MAX_USER_ID_LENGTH);
    
private intgetVersionNumber()
Answer the SOCKS version number. Should always be 4.

        return buffer[INDEX_VERSION];
    
public voidsetCommandOrResult(int command)
Set the request's command or result.

        buffer[INDEX_COMMAND] = (byte) command;
    
public voidsetIP(byte[] ip)
Set the IP address. This expects an array of four bytes in host order.

        buffer[INDEX_IP] = ip[0];
        buffer[INDEX_IP + 1] = ip[1];
        buffer[INDEX_IP + 2] = ip[2];
        buffer[INDEX_IP + 3] = ip[3];
    
private voidsetInt16(int offset, int value)
Put a 16 bit integer into the buffer at the offset given.

        buffer[offset] = (byte) (value >>> 8 & 0xFF);
        buffer[offset + 1] = (byte) (value & 0xFF);
    
public voidsetPort(int port)
Set the request's port number.

        setInt16(INDEX_PORT, port);
    
private voidsetString(int offset, int maxLength, java.lang.String theString)
Put a string into the buffer at the offset given.

        byte[] stringBytes;
        try {
            stringBytes = theString.getBytes("ISO8859_1"); //$NON-NLS-1$
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.toString());
        }
        int length = Math.min(stringBytes.length, maxLength);
        System.arraycopy(stringBytes, 0, buffer, offset, length);
        buffer[offset + length] = 0;
    
public voidsetUserId(java.lang.String id)
Set the user id for authentication.

        setString(INDEX_USER_ID, MAX_USER_ID_LENGTH, id);
    
private voidsetVersionNumber(int number)
Set the SOCKS version number. This should always be 4.

        buffer[INDEX_VERSION] = (byte) number;
    
public java.lang.StringtoString()

        StringBuilder buf = new StringBuilder(50);
        buf.append("Version: ");
        buf.append(Integer.toHexString(getVersionNumber()));
        buf.append(" Command: ");
        buf.append(Integer.toHexString(getCommandOrResult()));
        buf.append(" Port: ");
        buf.append(getPort());
        buf.append(" IP: ");
        buf.append(Integer.toHexString(getIP()));
        buf.append(" User ID: ");
        buf.append(getUserId());
        return buf.toString();