Mercurial > 510Connectbot
annotate src/net/sourceforge/jsocks/InetRange.java @ 427:f6d26c5f878e
WindowChange reverted to be similar to trilead - don't wait for a response to that request
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sun, 14 Dec 2014 19:00:19 -0800 |
parents | 205ee2873330 |
children |
rev | line source |
---|---|
349
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 package net.sourceforge.jsocks; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 import java.net.InetAddress; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 import java.net.UnknownHostException; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 import java.util.*; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 * Class InetRange provides the means of defining the range of inetaddresses. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 * It's used by Proxy class to store and look up addresses of machines, that |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 * should be contacted directly rather then through the proxy. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 * <P> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
11 * InetRange provides several methods to add either standalone addresses, or |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 * ranges (e.g. 100.200.300.0:100.200.300.255, which covers all addresses |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
13 * on on someones local network). It also provides methods for checking wether |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 * given address is in this range. Any number of ranges and standalone |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
15 * addresses can be added to the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
16 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
17 public class InetRange implements Cloneable{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
18 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
19 Hashtable host_names; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
20 Vector all; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
21 Vector end_names; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
22 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
23 boolean useSeparateThread = true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
24 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
25 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
26 * Creates the empty range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
27 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
28 public InetRange(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
29 all = new Vector(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
30 host_names = new Hashtable(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
31 end_names = new Vector(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
32 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
33 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
34 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
35 * Adds another host or range to this range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
36 The String can be one of those: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
37 <UL> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
38 <li> Host name. eg.(Athena.myhost.com or 45.54.56.65) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
39 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
40 <li> Range in the form .myhost.net.au <BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
41 In which case anything that ends with .myhost.net.au will |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
42 be considered in the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
43 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
44 <li> Range in the form ddd.ddd.ddd. <BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
45 This will be treated as range ddd.ddd.ddd.0 to ddd.ddd.ddd.255. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
46 It is not necessary to specify 3 first bytes you can use just |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
47 one or two. For example 130. will cover address between 130.0.0.0 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
48 and 13.255.255.255. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
49 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
50 <li> Range in the form host_from[: \t\n\r\f]host_to. <br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
51 That is two hostnames or ips separated by either whitespace |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
52 or colon. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
53 </UL> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
54 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
55 public synchronized boolean add(String s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
56 if(s == null) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
57 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
58 s = s.trim(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
59 if(s.length() == 0) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
60 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
61 Object[] entry; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
62 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
63 if(s.charAt(s.length()-1) == '.'){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
64 //thing like: 111.222.33. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
65 //it is being treated as range 111.222.33.000 - 111.222.33.255 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
66 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
67 int[] addr = ip2intarray(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
68 long from,to; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
69 from = to = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
70 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
71 if(addr == null) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
72 for(int i = 0; i< 4;++i){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
73 if(addr[i]>=0) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
74 from += (((long)addr[i]) << 8*(3-i)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
75 else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
76 to = from; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
77 while(i<4) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
78 to += 255l << 8*(3-i++); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
79 break; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
80 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
81 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
82 entry = new Object[] {s,null,new Long(from),new Long(to)}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
83 all.addElement(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
84 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
85 }else if(s.charAt(0) == '.'){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
86 //Thing like: .myhost.com |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
87 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
88 end_names.addElement(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
89 all.addElement(new Object[]{s,null,null,null}); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
90 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
91 StringTokenizer tokens = new StringTokenizer(s," \t\r\n\f:"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
92 if(tokens.countTokens() > 1){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
93 entry = new Object[] {s,null,null,null}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
94 resolve(entry,tokens.nextToken(),tokens.nextToken()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
95 all.addElement(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
96 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
97 entry = new Object[] {s,null,null,null}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
98 all.addElement(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
99 host_names.put(s,entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
100 resolve(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
101 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
102 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
103 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
104 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
105 return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
106 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
107 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
108 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
109 * Adds another ip for this range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
110 @param ip IP os the host which should be added to this range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
111 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
112 public synchronized void add(InetAddress ip){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
113 long from, to; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
114 from = to = ip2long(ip); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
115 all.addElement(new Object[]{ip.getHostName(),ip,new Long(from), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
116 new Long(to)}); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
117 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
118 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
119 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
120 * Adds another range of ips for this range.Any host with ip address |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
121 greater than or equal to the address of from and smaller than or equal |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
122 to the address of to will be included in the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
123 @param from IP from where range starts(including). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
124 @param to IP where range ends(including). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
125 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
126 public synchronized void add(InetAddress from,InetAddress to){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
127 all.addElement(new Object[]{from.getHostAddress()+":"+to.getHostAddress() |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
128 ,null,new Long(ip2long(from)), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
129 new Long(ip2long(to))}); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
130 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
131 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
132 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
133 * Checks wether the givan host is in the range. Attempts to resolve |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
134 host name if required. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
135 @param host Host name to check. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
136 @return true If host is in the range, false otherwise. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
137 * @see InetRange#contains(String,boolean) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
138 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
139 public synchronized boolean contains(String host){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
140 return contains(host,true); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
141 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
142 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
143 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
144 * Checks wether the given host is in the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
145 * <P> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
146 * Algorithm: <BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
147 * <ol> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
148 * <li>Look up if the hostname is in the range (in the Hashtable). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
149 * <li>Check if it ends with one of the speciefied endings. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
150 * <li>Check if it is ip(eg.130.220.35.98). If it is check if it is |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
151 * in the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
152 * <li>If attemptResolve is true, host is name, rather than ip, and |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
153 * all previous attempts failed, try to resolve the hostname, and |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
154 * check wether the ip associated with the host is in the range.It |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
155 * also repeats all previos steps with the hostname obtained from |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
156 * InetAddress, but the name is not allways the full name,it is |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
157 * quite likely to be the same. Well it was on my machine. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
158 * </ol> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
159 @param host Host name to check. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
160 @param attemptResolve Wether to lookup ip address which corresponds |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
161 to the host,if required. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
162 @return true If host is in the range, false otherwise. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
163 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
164 public synchronized boolean contains(String host,boolean attemptResolve){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
165 if(all.size() ==0) return false; //Empty range |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
166 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
167 host = host.trim(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
168 if(host.length() == 0) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
169 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
170 if(checkHost(host)) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
171 if(checkHostEnding(host)) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
172 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
173 long l = host2long(host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
174 if(l >=0) return contains(l); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
175 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
176 if(!attemptResolve) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
177 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
178 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
179 InetAddress ip = InetAddress.getByName(host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
180 return contains(ip); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
181 }catch(UnknownHostException uhe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
182 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
183 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
184 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
185 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
186 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
187 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
188 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
189 * Checks wether the given ip is in the range. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
190 @param ip Address of the host to check. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
191 @return true If host is in the range, false otherwise. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
192 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
193 public synchronized boolean contains(InetAddress ip){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
194 if(checkHostEnding(ip.getHostName())) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
195 if(checkHost(ip.getHostName())) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
196 return contains(ip2long(ip)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
197 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
198 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
199 Get all entries in the range as strings. <BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
200 These strings can be used to delete entries from the range |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
201 with remove function. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
202 @return Array of entries as strings. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
203 @see InetRange#remove(String) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
204 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
205 public synchronized String[] getAll(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
206 int size = all.size(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
207 Object entry[]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
208 String all_names[] = new String[size]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
209 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
210 for(int i=0;i<size;++i){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
211 entry = (Object[]) all.elementAt(i); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
212 all_names[i] = (String) entry[0]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
213 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
214 return all_names; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
215 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
216 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
217 Removes an entry from this range.<BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
218 @param s Entry to remove. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
219 @return true if successfull. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
220 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
221 public synchronized boolean remove(String s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
222 Enumeration eEnum = all.elements(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
223 while(eEnum.hasMoreElements()){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
224 Object[] entry = (Object[]) eEnum.nextElement(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
225 if(s.equals(entry[0])){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
226 all.removeElement(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
227 end_names.removeElement(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
228 host_names.remove(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
229 return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
230 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
231 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
232 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
233 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
234 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
235 /** Get string representaion of this Range.*/ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
236 public String toString(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
237 String all[] = getAll(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
238 if(all.length == 0) return ""; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
239 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
240 String s = all[0]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
241 for(int i=1;i<all.length;++i) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
242 s += "; "+all[i]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
243 return s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
244 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
245 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
246 /** Creates a clone of this Object*/ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
247 public Object clone(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
248 InetRange new_range = new InetRange(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
249 new_range.all = (Vector)all.clone(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
250 new_range.end_names = (Vector) end_names.clone(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
251 new_range.host_names = (Hashtable)host_names.clone(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
252 return new_range; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
253 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
254 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
255 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
256 //Private methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
257 ///////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
258 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
259 * Same as previous but used internally, to avoid |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
260 * unnecessary convertion of IPs, when checking subranges |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
261 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
262 private synchronized boolean contains(long ip){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
263 Enumeration eEnum = all.elements(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
264 while(eEnum.hasMoreElements()){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
265 Object[] obj = (Object[]) eEnum.nextElement(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
266 Long from = obj[2]==null?null:(Long)obj[2]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
267 Long to = obj[3]==null?null:(Long)obj[3]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
268 if(from != null && from.longValue()<= ip |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
269 && to.longValue() >= ip) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
270 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
271 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
272 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
273 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
274 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
275 private boolean checkHost(String host){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
276 return host_names.containsKey(host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
277 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
278 private boolean checkHostEnding(String host){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
279 Enumeration eEnum = end_names.elements(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
280 while(eEnum.hasMoreElements()){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
281 if(host.endsWith((String) eEnum.nextElement())) return true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
282 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
283 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
284 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
285 private void resolve(Object[] entry){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
286 //First check if it's in the form ddd.ddd.ddd.ddd. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
287 long ip = host2long((String) entry[0]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
288 if(ip >= 0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
289 entry[2] = entry[3] = new Long(ip); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
290 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
291 InetRangeResolver res = new InetRangeResolver(entry); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
292 res.resolve(useSeparateThread); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
293 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
294 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
295 private void resolve(Object[] entry,String from,String to){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
296 long f,t; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
297 if((f=host2long(from))>= 0 && (t=host2long(to)) >= 0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
298 entry[2] = new Long(f); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
299 entry[3] = new Long(t); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
300 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
301 InetRangeResolver res = new InetRangeResolver(entry,from,to); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
302 res.resolve(useSeparateThread); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
303 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
304 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
305 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
306 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
307 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
308 //Class methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
309 /////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
310 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
311 //Converts ipv4 to long value(unsigned int) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
312 /////////////////////////////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
313 static long ip2long(InetAddress ip){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
314 long l=0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
315 byte[] addr = ip.getAddress(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
316 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
317 if(addr.length ==4){ //IPV4 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
318 for(int i=0;i<4;++i) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
319 l += (((long)addr[i] &0xFF) << 8*(3-i)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
320 }else{ //IPV6 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
321 return 0; //Have no idea how to deal with those |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
322 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
323 return l; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
324 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
325 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
326 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
327 long host2long(String host){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
328 long ip=0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
329 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
330 //check if it's ddd.ddd.ddd.ddd |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
331 if(!Character.isDigit(host.charAt(0))) return -1; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
332 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
333 int[] addr = ip2intarray(host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
334 if(addr == null) return -1; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
335 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
336 for(int i=0;i<addr.length;++i) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
337 ip += ((long)(addr[i]>=0 ? addr[i] : 0)) << 8*(3-i); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
338 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
339 return ip; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
340 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
341 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
342 static int[] ip2intarray(String host){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
343 int[] address = {-1,-1,-1,-1}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
344 int i=0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
345 StringTokenizer tokens = new StringTokenizer(host,"."); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
346 if(tokens.countTokens() > 4) return null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
347 while(tokens.hasMoreTokens()){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
348 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
349 address[i++] = Integer.parseInt(tokens.nextToken()) & 0xFF; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
350 }catch(NumberFormatException nfe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
351 return null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
352 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
353 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
354 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
355 return address; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
356 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
357 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
358 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
359 /* |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
360 //* This was the test main function |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
361 //********************************** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
362 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
363 public static void main(String args[])throws UnknownHostException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
364 int i; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
365 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
366 InetRange ir = new InetRange(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
367 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
368 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
369 for(i=0;i<args.length;++i){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
370 System.out.println("Adding:" + args[i]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
371 ir.add(args[i]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
372 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
373 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
374 String host; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
375 java.io.DataInputStream din = new java.io.DataInputStream(System.in); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
376 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
377 host = din.readLine(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
378 while(host!=null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
379 if(ir.contains(host)){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
380 System.out.println("Range contains ip:"+host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
381 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
382 System.out.println(host+" is not in the range"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
383 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
384 host = din.readLine(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
385 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
386 }catch(java.io.IOException io_ex){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
387 io_ex.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
388 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
389 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
390 ********************/ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
391 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
392 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
393 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
394 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
395 class InetRangeResolver implements Runnable{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
396 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
397 Object[] entry; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
398 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
399 String from, to; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
400 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
401 InetRangeResolver(Object[] entry){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
402 this.entry = entry; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
403 from = to = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
404 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
405 InetRangeResolver(Object[] entry,String from,String to){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
406 this.entry = entry; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
407 this.from = from; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
408 this.to = to; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
409 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
410 public final void resolve(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
411 resolve(true); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
412 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
413 public final void resolve(boolean inSeparateThread){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
414 if(inSeparateThread){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
415 Thread t = new Thread(this); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
416 t.start(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
417 }else |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
418 run(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
419 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
420 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
421 public void run(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
422 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
423 if(from == null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
424 InetAddress ip = InetAddress.getByName((String) entry[0]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
425 entry[1] = ip; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
426 Long l = new Long(InetRange.ip2long(ip)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
427 entry[2] = entry[3] = l; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
428 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
429 InetAddress f = InetAddress.getByName(from); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
430 InetAddress t = InetAddress.getByName(to); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
431 entry[2] = new Long(InetRange.ip2long(f)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
432 entry[3] = new Long(InetRange.ip2long(t)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
433 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
434 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
435 }catch(UnknownHostException uhe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
436 //System.err.println("Resolve failed for "+from+','+to+','+entry[0]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
437 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
438 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
439 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
440 } |