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 org.jboss.netty.logging.InternalLogger;
19 import org.jboss.netty.logging.InternalLoggerFactory;
20 import org.jboss.netty.util.internal.StringUtil;
21
22 import java.net.InetAddress;
23 import java.net.UnknownHostException;
24 import java.util.StringTokenizer;
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 String[] tokens = StringUtil.split(netAddress, '/');
103 if (tokens.length != 2) {
104 throw new IllegalArgumentException("netAddress: " + netAddress + " (expected: CIDR or Decimal Notation)");
105 }
106
107 if (tokens[1].length() < 3) {
108 setNetId(tokens[0]);
109 setCidrNetMask(Integer.parseInt(tokens[1]));
110 } else {
111 setNetId(tokens[0]);
112 setNetMask(tokens[1]);
113 }
114 }
115
116
117 private void setNetAddress(InetAddress inetAddress, int cidrNetMask) {
118 setNetId(inetAddress);
119 setCidrNetMask(cidrNetMask);
120 }
121
122
123 private void setNetAddress(InetAddress inetAddress, String netMask) {
124 setNetId(inetAddress);
125 setNetMask(netMask);
126 }
127
128
129
130
131
132
133
134 private void setNetId(String netId) throws UnknownHostException {
135 InetAddress inetAddress1 = InetAddress.getByName(netId);
136 setNetId(inetAddress1);
137 }
138
139
140
141
142
143
144 private static int toInt(InetAddress inetAddress1) {
145 byte[] address = inetAddress1.getAddress();
146 int net = 0;
147 for (byte addres : address) {
148 net <<= 8;
149 net |= addres & BYTE_ADDRESS_MASK;
150 }
151 return net;
152 }
153
154
155 private void setNetId(InetAddress inetAddress) {
156 this.inetAddress = inetAddress;
157 subnet = toInt(inetAddress);
158 }
159
160
161
162
163
164
165
166 private void setNetMask(String netMask) {
167 StringTokenizer nm = new StringTokenizer(netMask, ".");
168 int i = 0;
169 int[] netmask = new int[4];
170 while (nm.hasMoreTokens()) {
171 netmask[i] = Integer.parseInt(nm.nextToken());
172 i++;
173 }
174 int mask1 = 0;
175 for (i = 0; i < 4; i++) {
176 mask1 += Integer.bitCount(netmask[i]);
177 }
178 setCidrNetMask(mask1);
179 }
180
181
182
183
184
185
186
187 private void setCidrNetMask(int cidrNetMask) {
188 cidrMask = cidrNetMask;
189 mask = SUBNET_MASK >> cidrMask - 1;
190 }
191
192
193
194
195
196
197
198
199
200 public boolean contains(String ipAddr) throws UnknownHostException {
201 InetAddress inetAddress1 = InetAddress.getByName(ipAddr);
202 return contains(inetAddress1);
203 }
204
205
206
207
208
209
210
211
212 public boolean contains(InetAddress inetAddress1) {
213 if (mask == -1) {
214
215 return true;
216 }
217 return (toInt(inetAddress1) & mask) == subnet;
218 }
219
220 @Override
221 public String toString() {
222 return inetAddress.getHostAddress() + '/' + cidrMask;
223 }
224
225 @Override
226 public boolean equals(Object o) {
227 if (!(o instanceof IpV4Subnet)) {
228 return false;
229 }
230 IpV4Subnet ipV4Subnet = (IpV4Subnet) o;
231 return ipV4Subnet.subnet == subnet && ipV4Subnet.cidrMask == cidrMask;
232 }
233
234 @Override
235 public int hashCode() {
236 return subnet;
237 }
238
239
240 public int compareTo(IpV4Subnet o) {
241 if (o.subnet == subnet && o.cidrMask == cidrMask) {
242 return 0;
243 }
244 if (o.subnet < subnet) {
245 return 1;
246 } else if (o.subnet > subnet) {
247 return -1;
248 } else if (o.cidrMask < cidrMask) {
249
250 return -1;
251 }
252 return 1;
253 }
254 }