1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.util;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22
23
24
25
26
27
28 public class SubnetUtils {
29
30 private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";
31 private static final String SLASH_FORMAT = IP_ADDRESS + "/(\\d{1,3})";
32 private static final Pattern addressPattern = Pattern.compile(IP_ADDRESS);
33 private static final Pattern cidrPattern = Pattern.compile(SLASH_FORMAT);
34 private static final int NBITS = 32;
35
36 private int netmask = 0;
37 private int address = 0;
38 private int network = 0;
39 private int broadcast = 0;
40
41
42 private boolean inclusiveHostCount = false;
43
44
45
46
47
48
49
50
51 public SubnetUtils(String cidrNotation) {
52 calculate(cidrNotation);
53 }
54
55
56
57
58
59
60
61
62 public SubnetUtils(String address, String mask) {
63 calculate(toCidrNotation(address, mask));
64 }
65
66
67
68
69
70
71
72 public boolean isInclusiveHostCount() {
73 return inclusiveHostCount;
74 }
75
76
77
78
79
80
81
82 public void setInclusiveHostCount(boolean inclusiveHostCount) {
83 this.inclusiveHostCount = inclusiveHostCount;
84 }
85
86
87
88
89
90
91
92 public final class SubnetInfo {
93 private SubnetInfo() {}
94
95 private int netmask() { return netmask; }
96 private int network() { return network; }
97 private int address() { return address; }
98 private int broadcast() { return broadcast; }
99
100 private int low() {
101 return (isInclusiveHostCount() ? network() :
102 broadcast() - network() > 1 ? network() + 1 : 0);
103 }
104
105 private int high() {
106 return (isInclusiveHostCount() ? broadcast() :
107 broadcast() - network() > 1 ? broadcast() -1 : 0);
108 }
109
110
111
112
113
114
115
116
117 public boolean isInRange(String address) {
118 return isInRange(toInteger(address));
119 }
120
121 private boolean isInRange(int address) {
122 int diff = address - low();
123 return (diff >= 0 && (diff <= (high() - low())));
124 }
125
126 public String getBroadcastAddress() {
127 return format(toArray(broadcast()));
128 }
129
130 public String getNetworkAddress() {
131 return format(toArray(network()));
132 }
133
134 public String getNetmask() {
135 return format(toArray(netmask()));
136 }
137
138 public String getAddress() {
139 return format(toArray(address()));
140 }
141
142
143
144
145
146
147
148 public String getLowAddress() {
149 return format(toArray(low()));
150 }
151
152
153
154
155
156
157
158 public String getHighAddress() {
159 return format(toArray(high()));
160 }
161
162
163
164
165
166
167 public int getAddressCount() {
168 int count = broadcast() - network() + (isInclusiveHostCount() ? 1 : -1);
169 return count < 0 ? 0 : count;
170 }
171
172 public int asInteger(String address) {
173 return toInteger(address);
174 }
175
176 public String getCidrSignature() {
177 return toCidrNotation(
178 format(toArray(address())),
179 format(toArray(netmask()))
180 );
181 }
182
183 public String[] getAllAddresses() {
184 int ct = getAddressCount();
185 String[] addresses = new String[ct];
186 if (ct == 0) {
187 return addresses;
188 }
189 for (int add = low(), j=0; add <= high(); ++add, ++j) {
190 addresses[j] = format(toArray(add));
191 }
192 return addresses;
193 }
194
195
196
197
198
199 @Override
200 public String toString() {
201 final StringBuilder buf = new StringBuilder();
202 buf.append("CIDR Signature:\t[").append(getCidrSignature()).append("]")
203 .append(" Netmask: [").append(getNetmask()).append("]\n")
204 .append("Network:\t[").append(getNetworkAddress()).append("]\n")
205 .append("Broadcast:\t[").append(getBroadcastAddress()).append("]\n")
206 .append("First Address:\t[").append(getLowAddress()).append("]\n")
207 .append("Last Address:\t[").append(getHighAddress()).append("]\n")
208 .append("# Addresses:\t[").append(getAddressCount()).append("]\n");
209 return buf.toString();
210 }
211 }
212
213
214
215
216
217 public final SubnetInfo getInfo() { return new SubnetInfo(); }
218
219
220
221
222 private void calculate(String mask) {
223 Matcher matcher = cidrPattern.matcher(mask);
224
225 if (matcher.matches()) {
226 address = matchAddress(matcher);
227
228
229 int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS);
230 for (int j = 0; j < cidrPart; ++j) {
231 netmask |= (1 << 31-j);
232 }
233
234
235 network = (address & netmask);
236
237
238 broadcast = network | ~(netmask);
239 } else {
240 throw new IllegalArgumentException("Could not parse [" + mask + "]");
241 }
242 }
243
244
245
246
247 private int toInteger(String address) {
248 Matcher matcher = addressPattern.matcher(address);
249 if (matcher.matches()) {
250 return matchAddress(matcher);
251 } else {
252 throw new IllegalArgumentException("Could not parse [" + address + "]");
253 }
254 }
255
256
257
258
259
260 private int matchAddress(Matcher matcher) {
261 int addr = 0;
262 for (int i = 1; i <= 4; ++i) {
263 int n = (rangeCheck(Integer.parseInt(matcher.group(i)), -1, 255));
264 addr |= ((n & 0xff) << 8*(4-i));
265 }
266 return addr;
267 }
268
269
270
271
272 private int[] toArray(int val) {
273 int ret[] = new int[4];
274 for (int j = 3; j >= 0; --j) {
275 ret[j] |= ((val >>> 8*(3-j)) & (0xff));
276 }
277 return ret;
278 }
279
280
281
282
283 private String format(int[] octets) {
284 StringBuilder str = new StringBuilder();
285 for (int i =0; i < octets.length; ++i){
286 str.append(octets[i]);
287 if (i != octets.length - 1) {
288 str.append(".");
289 }
290 }
291 return str.toString();
292 }
293
294
295
296
297
298
299 private int rangeCheck(int value, int begin, int end) {
300 if (value > begin && value <= end) {
301 return value;
302 }
303
304 throw new IllegalArgumentException("Value [" + value + "] not in range ("+begin+","+end+"]");
305 }
306
307
308
309
310
311 int pop(int x) {
312 x = x - ((x >>> 1) & 0x55555555);
313 x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
314 x = (x + (x >>> 4)) & 0x0F0F0F0F;
315 x = x + (x >>> 8);
316 x = x + (x >>> 16);
317 return x & 0x0000003F;
318 }
319
320
321
322
323
324 private String toCidrNotation(String addr, String mask) {
325 return addr + "/" + pop(toInteger(mask));
326 }
327 }