1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.ipfilter;
17
18 import java.net.InetAddress;
19 import java.net.UnknownHostException;
20 import java.util.StringTokenizer;
21 import java.util.Vector;
22
23 import org.jboss.netty.logging.InternalLogger;
24 import org.jboss.netty.logging.InternalLoggerFactory;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class IpV4Subnet implements IpSet, Comparable<IpV4Subnet> {
46
47 private static final InternalLogger logger =
48 InternalLoggerFactory.getInstance(IpV4Subnet.class);
49
50 private static final int SUBNET_MASK = 0x80000000;
51
52 private static final int BYTE_ADDRESS_MASK = 0xFF;
53
54 private InetAddress inetAddress;
55
56 private int subnet;
57
58 private int mask;
59
60 private int cidrMask;
61
62
63 public IpV4Subnet() {
64
65 mask = -1;
66
67 inetAddress = null;
68 subnet = 0;
69 cidrMask = 0;
70 }
71
72
73
74
75
76
77
78
79
80 public IpV4Subnet(String netAddress) throws UnknownHostException {
81 setNetAddress(netAddress);
82 }
83
84
85 public IpV4Subnet(InetAddress inetAddress, int cidrNetMask) {
86 setNetAddress(inetAddress, cidrNetMask);
87 }
88
89
90 public IpV4Subnet(InetAddress inetAddress, String netMask) {
91 setNetAddress(inetAddress, netMask);
92 }
93
94
95
96
97
98
99
100
101 private void setNetAddress(String netAddress) throws UnknownHostException {
102 Vector<Object> vec = new Vector<Object>();
103 StringTokenizer st = new StringTokenizer(netAddress, "/");
104 while (st.hasMoreTokens()) {
105 vec.add(st.nextElement());
106 }
107
108 if (vec.get(1).toString().length() < 3) {
109 setNetId(vec.get(0).toString());
110 setCidrNetMask(Integer.parseInt(vec.get(1).toString()));
111 } else {
112 setNetId(vec.get(0).toString());
113 setNetMask(vec.get(1).toString());
114 }
115 }
116
117
118 private void setNetAddress(InetAddress inetAddress, int cidrNetMask) {
119 setNetId(inetAddress);
120 setCidrNetMask(cidrNetMask);
121 }
122
123
124 private void setNetAddress(InetAddress inetAddress, String netMask) {
125 setNetId(inetAddress);
126 setNetMask(netMask);
127 }
128
129
130
131
132
133
134
135 private void setNetId(String netId) throws UnknownHostException {
136 InetAddress inetAddress1 = InetAddress.getByName(netId);
137 this.setNetId(inetAddress1);
138 }
139
140
141
142
143
144
145 private static int toInt(InetAddress inetAddress1) {
146 byte[] address = inetAddress1.getAddress();
147 int net = 0;
148 for (byte addres : address) {
149 net <<= 8;
150 net |= addres & BYTE_ADDRESS_MASK;
151 }
152 return net;
153 }
154
155
156 private void setNetId(InetAddress inetAddress) {
157 this.inetAddress = inetAddress;
158 subnet = toInt(inetAddress);
159 }
160
161
162
163
164
165
166
167 private void setNetMask(String netMask) {
168 StringTokenizer nm = new StringTokenizer(netMask, ".");
169 int i = 0;
170 int[] netmask = new int[4];
171 while (nm.hasMoreTokens()) {
172 netmask[i] = Integer.parseInt(nm.nextToken());
173 i++;
174 }
175 int mask1 = 0;
176 for (i = 0; i < 4; i++) {
177 mask1 += Integer.bitCount(netmask[i]);
178 }
179 setCidrNetMask(mask1);
180 }
181
182
183
184
185
186
187
188 private void setCidrNetMask(int cidrNetMask) {
189 cidrMask = cidrNetMask;
190 mask = SUBNET_MASK >> cidrMask - 1;
191 }
192
193
194
195
196
197
198
199
200
201 public boolean contains(String ipAddr) throws UnknownHostException {
202 InetAddress inetAddress1 = InetAddress.getByName(ipAddr);
203 return this.contains(inetAddress1);
204 }
205
206
207
208
209
210
211
212
213 public boolean contains(InetAddress inetAddress1) {
214 if (mask == -1) {
215
216 return true;
217 }
218 return (toInt(inetAddress1) & mask) == subnet;
219 }
220
221 @Override
222 public String toString() {
223 return inetAddress.getHostAddress() + "/" + cidrMask;
224 }
225
226 @Override
227 public boolean equals(Object o) {
228 if (!(o instanceof IpV4Subnet)) {
229 return false;
230 }
231 IpV4Subnet ipV4Subnet = (IpV4Subnet) o;
232 return ipV4Subnet.subnet == subnet && ipV4Subnet.cidrMask == cidrMask;
233 }
234
235 @Override
236 public int hashCode() {
237 return subnet;
238 }
239
240
241 public int compareTo(IpV4Subnet o) {
242 if (o.subnet == subnet && o.cidrMask == cidrMask) {
243 return 0;
244 }
245 if (o.subnet < subnet) {
246 return 1;
247 } else if (o.subnet > subnet) {
248 return -1;
249 } else if (o.cidrMask < cidrMask) {
250
251 return -1;
252 }
253 return 1;
254 }
255 }