IpPrefix.java
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.onlab.packet;
import java.util.Arrays;
/**
* A class representing an IPv4 prefix.
* <p/>
* A prefix consists of an IP address and a subnet mask.
*/
public final class IpPrefix {
// TODO a comparator for netmasks? E.g. for sorting by prefix match order.
//IP Versions
public enum Version { INET, INET6 };
//lengths of address, in bytes
public static final int INET_LEN = 4;
public static final int INET6_LEN = 16;
//maximum CIDR value
public static final int MAX_INET_MASK = 32;
//no mask (no network), e.g. a simple address
public static final int DEFAULT_MASK = 0;
/**
* Default value indicating an unspecified address.
*/
static final byte[] ANY = new byte [] {0, 0, 0, 0};
protected Version version;
protected byte[] octets;
protected int netmask;
private IpPrefix(Version ver, byte[] octets, int netmask) {
this.version = ver;
this.octets = Arrays.copyOf(octets, INET_LEN);
this.netmask = netmask;
}
private IpPrefix(Version ver, byte[] octets) {
this.version = ver;
this.octets = Arrays.copyOf(octets, INET_LEN);
this.netmask = DEFAULT_MASK;
}
/**
* Converts a byte array into an IP address.
*
* @param address a byte array
* @return an IP address
*/
public static IpPrefix valueOf(byte [] address) {
return new IpPrefix(Version.INET, address);
}
/**
* Converts a byte array into an IP address.
*
* @param address a byte array
* @param netmask the CIDR value subnet mask
* @return an IP address
*/
public static IpPrefix valueOf(byte [] address, int netmask) {
return new IpPrefix(Version.INET, address, netmask);
}
/**
* Helper to convert an integer into a byte array.
*
* @param address the integer to convert
* @return a byte array
*/
private static byte [] bytes(int address) {
byte [] bytes = new byte [INET_LEN];
for (int i = 0; i < INET_LEN; i++) {
bytes[i] = (byte) ((address >> (INET_LEN - (i + 1)) * 8) & 0xff);
}
return bytes;
}
/**
* Converts an integer into an IPv4 address.
*
* @param address an integer representing an IP value
* @return an IP address
*/
public static IpPrefix valueOf(int address) {
return new IpPrefix(Version.INET, bytes(address));
}
/**
* Converts an integer into an IPv4 address.
*
* @param address an integer representing an IP value
* @param netmask the CIDR value subnet mask
* @return an IP address
*/
public static IpPrefix valueOf(int address, int netmask) {
return new IpPrefix(Version.INET, bytes(address), netmask);
}
/**
* Converts a dotted-decimal string (x.x.x.x) into an IPv4 address. The
* string can also be in CIDR (slash) notation. If the netmask is omitted,
* it will be set to DEFAULT_MASK (0).
*
* @param address a IP address in string form, e.g. "10.0.0.1", "10.0.0.1/24"
* @return an IP address
*/
public static IpPrefix valueOf(String address) {
final String [] parts = address.split("\\/");
if (parts.length > 2) {
throw new IllegalArgumentException("Malformed IP address string; "
+ "Address must take form \"x.x.x.x\" or \"x.x.x.x/y\"");
}
int mask = DEFAULT_MASK;
if (parts.length == 2) {
mask = Integer.parseInt(parts[1]);
if (mask > MAX_INET_MASK) {
throw new IllegalArgumentException(
"Value of subnet mask cannot exceed "
+ MAX_INET_MASK);
}
}
final String [] net = parts[0].split("\\.");
if (net.length != INET_LEN) {
throw new IllegalArgumentException("Malformed IP address string; "
+ "Address must have four decimal values separated by dots (.)");
}
final byte [] bytes = new byte[INET_LEN];
for (int i = 0; i < INET_LEN; i++) {
bytes[i] = (byte) Short.parseShort(net[i], 10);
}
return new IpPrefix(Version.INET, bytes, mask);
}
/**
* Returns the IP version of this address.
*
* @return the version
*/
public Version version() {
return this.version;
}
/**
* Returns the IP address as a byte array.
*
* @return a byte array
*/
public byte[] toOctets() {
return Arrays.copyOf(this.octets, INET_LEN);
}
/**
* Returns the IP address prefix length.
*
* @return prefix length
*/
public int prefixLength() {
return netmask;
}
/**
* Returns the integral value of this IP address.
*
* @return the IP address's value as an integer
*/
public int toInt() {
int val = 0;
for (int i = 0; i < octets.length; i++) {
val <<= 8;
val |= octets[i] & 0xff;
}
return val;
}
/**
* Helper for computing the mask value from CIDR.
*
* @return an integer bitmask
*/
private int mask() {
int shift = MAX_INET_MASK - this.netmask;
return ((Integer.MAX_VALUE >>> (shift - 1)) << shift);
}
/**
* Returns the subnet mask in IpAddress form. The netmask value for
* the returned IpAddress is 0, as the address itself is a mask.
*
* @return the subnet mask
*/
public IpPrefix netmask() {
return new IpPrefix(Version.INET, bytes(mask()));
}
/**
* Returns the network portion of this address as an IpAddress.
* The netmask of the returned IpAddress is the current mask. If this
* address doesn't have a mask, this returns an all-0 IpAddress.
*
* @return the network address or null
*/
public IpPrefix network() {
if (netmask == DEFAULT_MASK) {
return new IpPrefix(version, ANY, DEFAULT_MASK);
}
byte [] net = new byte [4];
byte [] mask = bytes(mask());
for (int i = 0; i < INET_LEN; i++) {
net[i] = (byte) (octets[i] & mask[i]);
}
return new IpPrefix(version, net, netmask);
}
/**
* Returns the host portion of the IPAddress, as an IPAddress.
* The netmask of the returned IpAddress is the current mask. If this
* address doesn't have a mask, this returns a copy of the current
* address.
*
* @return the host address
*/
public IpPrefix host() {
if (netmask == DEFAULT_MASK) {
new IpPrefix(version, octets, netmask);
}
byte [] host = new byte [INET_LEN];
byte [] mask = bytes(mask());
for (int i = 0; i < INET_LEN; i++) {
host[i] = (byte) (octets[i] & ~mask[i]);
}
return new IpPrefix(version, host, netmask);
}
/**
* Returns an IpAddress of the bytes contained in this prefix.
* FIXME this is a hack for now and only works because IpPrefix doesn't
* mask the input bytes on creation.
*
* @return the IpAddress
*/
public IpAddress toIpAddress() {
return IpAddress.valueOf(octets);
}
public boolean isMasked() {
return mask() != 0;
}
/**
* Determines whether a given address is contained within this IpAddress'
* network.
*
* @param other another IP address that could be contained in this network
* @return true if the other IP address is contained in this address'
* network, otherwise false
*/
public boolean contains(IpPrefix other) {
if (this.netmask <= other.netmask) {
// Special case where they're both /32 addresses
if (this.netmask == MAX_INET_MASK) {
return Arrays.equals(octets, other.octets);
}
// Mask the other address with our network mask
IpPrefix otherMasked =
IpPrefix.valueOf(other.octets, netmask).network();
return network().equals(otherMasked);
}
return false;
}
public boolean contains(IpAddress address) {
// Need to get the network address because prefixes aren't automatically
// masked on creation
IpPrefix meMasked = network();
IpPrefix otherMasked =
IpPrefix.valueOf(address.octets, netmask).network();
return Arrays.equals(meMasked.octets, otherMasked.octets);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + netmask;
result = prime * result + Arrays.hashCode(octets);
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
IpPrefix other = (IpPrefix) obj;
if (netmask != other.netmask) {
return false;
}
// TODO not quite right until we mask the input
if (!Arrays.equals(octets, other.octets)) {
return false;
}
if (version != other.version) {
return false;
}
return true;
}
@Override
/*
* (non-Javadoc)
* format is "x.x.x.x" for non-masked (netmask 0) addresses,
* and "x.x.x.x/y" for masked addresses.
*
* @see java.lang.Object#toString()
*/
public String toString() {
final StringBuilder builder = new StringBuilder();
for (final byte b : this.octets) {
if (builder.length() > 0) {
builder.append(".");
}
builder.append(String.format("%d", b & 0xff));
}
if (netmask != DEFAULT_MASK) {
builder.append("/");
builder.append(String.format("%d", netmask));
}
return builder.toString();
}
}