comparison src/net/sourceforge/jsocks/ProxyMessage.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children 205ee2873330
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1 package net.sourceforge.jsocks;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.UnknownHostException;
8
9 /**
10 Abstract class which describes SOCKS4/5 response/request.
11 */
12 public abstract class ProxyMessage {
13 /** Host as an IP address */
14 public InetAddress ip = null;
15 /** SOCKS version, or version of the response for SOCKS4*/
16 public int version;
17 /** Port field of the request/response*/
18 public int port;
19 /** Request/response code as an int*/
20 public int command;
21 /** Host as string.*/
22 public String host = null;
23 /** User field for SOCKS4 request messages*/
24 public String user = null;
25
26 ProxyMessage(int command, InetAddress ip, int port) {
27 this.command = command;
28 this.ip = ip;
29 this.port = port;
30 }
31
32 ProxyMessage() {
33 }
34
35
36 /**
37 Initialises Message from the stream. Reads server response from
38 given stream.
39 @param in Input stream to read response from.
40 @throws SocksException If server response code is not SOCKS_SUCCESS(0), or
41 if any error with protocol occurs.
42 @throws IOException If any error happens with I/O.
43 */
44 public abstract void read(InputStream in)
45 throws SocksException,
46 IOException;
47
48
49 /**
50 Initialises Message from the stream. Reads server response or client
51 request from given stream.
52
53 @param in Input stream to read response from.
54 @param clinetMode If true read server response, else read client request.
55 @throws SocksException If server response code is not SOCKS_SUCCESS(0) and
56 reading in client mode, or if any error with protocol occurs.
57 @throws IOException If any error happens with I/O.
58 */
59 public abstract void read(InputStream in, boolean client_mode)
60 throws SocksException,
61 IOException;
62
63
64 /**
65 Writes the message to the stream.
66 @param out Output stream to which message should be written.
67 */
68 public abstract void write(OutputStream out)throws SocksException,
69 IOException;
70
71 /**
72 Get the Address field of this message as InetAddress object.
73 @return Host address or null, if one can't be determined.
74 */
75 public InetAddress getInetAddress() throws UnknownHostException {
76 return ip;
77 }
78
79
80 /**
81 Get string representaion of this message.
82 @return string representation of this message.
83 */
84 public String toString() {
85 return
86 "Proxy Message:\n" +
87 "Version:" + version + "\n" +
88 "Command:" + command + "\n" +
89 "IP: " + ip + "\n" +
90 "Port: " + port + "\n" +
91 "User: " + user + "\n" ;
92 }
93
94 //Package methods
95 //////////////////
96
97 static final String bytes2IPV4(byte[] addr, int offset) {
98 String hostName = "" + (addr[offset] & 0xFF);
99
100 for (int i = offset + 1; i < offset + 4; ++i)
101 hostName += "." + (addr[i] & 0xFF);
102
103 return hostName;
104 }
105
106 static final String bytes2IPV6(byte[] addr, int offset) {
107 //Have no idea how they look like!
108 return null;
109 }
110
111 }