KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > socks > ProxyMessage


1 package socks;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.io.DataInputStream JavaDoc;
7 import java.net.InetAddress JavaDoc;
8 import java.net.UnknownHostException JavaDoc;
9
10 /**
11  Abstract class which describes SOCKS4/5 response/request.
12 */

13 public abstract class ProxyMessage{
14    /** Host as an IP address */
15    public InetAddress JavaDoc ip=null;
16    /** SOCKS version, or version of the response for SOCKS4*/
17    public int version;
18    /** Port field of the request/response*/
19    public int port;
20    /** Request/response code as an int*/
21    public int command;
22    /** Host as string.*/
23    public String JavaDoc host=null;
24    /** User field for SOCKS4 request messages*/
25    public String JavaDoc user=null;
26
27    ProxyMessage(int command,InetAddress JavaDoc ip,int port){
28       this.command = command;
29       this.ip = ip;
30       this.port = port;
31    }
32
33    ProxyMessage(){
34    }
35
36
37    /**
38      Initialises Message from the stream. Reads server response from
39      given stream.
40      @param in Input stream to read response from.
41      @throws SocksException If server response code is not SOCKS_SUCCESS(0), or
42      if any error with protocol occurs.
43      @throws IOException If any error happens with I/O.
44    */

45    public abstract void read(InputStream JavaDoc in)
46                                     throws SocksException,
47                                            IOException JavaDoc;
48
49
50    /**
51      Initialises Message from the stream. Reads server response or client
52      request from given stream.
53      
54      @param in Input stream to read response from.
55      @param clinetMode If true read server response, else read client request.
56      @throws SocksException If server response code is not SOCKS_SUCCESS(0) and
57      reading in client mode, or if any error with protocol occurs.
58      @throws IOException If any error happens with I/O.
59    */

60    public abstract void read(InputStream JavaDoc in,boolean client_mode)
61                                     throws SocksException,
62                                            IOException JavaDoc;
63
64
65    /**
66     Writes the message to the stream.
67     @param out Output stream to which message should be written.
68    */

69    public abstract void write(OutputStream JavaDoc out)throws SocksException,
70                                              IOException JavaDoc;
71
72    /**
73     Get the Address field of this message as InetAddress object.
74     @return Host address or null, if one can't be determined.
75    */

76    public InetAddress JavaDoc getInetAddress() throws UnknownHostException JavaDoc{
77      return ip;
78    }
79
80
81    /**
82     Get string representaion of this message.
83     @return string representation of this message.
84    */

85    public String JavaDoc toString(){
86       return
87       "Proxy Message:\n"+
88       "Version:"+ version+"\n"+
89       "Command:"+ command+"\n"+
90       "IP: "+ ip+"\n"+
91       "Port: "+ port+"\n"+
92       "User: "+ user+"\n" ;
93    }
94
95 //Package methods
96
//////////////////
97

98    static final String JavaDoc bytes2IPV4(byte[] addr,int offset){
99       String JavaDoc hostName = ""+(addr[offset] & 0xFF);
100       for(int i = offset+1;i<offset+4;++i)
101         hostName+="."+(addr[i] & 0xFF);
102       return hostName;
103    }
104
105    static final String JavaDoc bytes2IPV6(byte[] addr,int offset){
106      //Have no idea how they look like!
107
return null;
108    }
109
110 }
111
Popular Tags