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