annotate src/net/sourceforge/jsocks/ProxyMessage.java @ 416:07c1da90deab

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