Ip6Address.java
8.62 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
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed 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.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Objects;
import com.google.common.net.InetAddresses;
import com.google.common.primitives.UnsignedLongs;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* The class representing an IPv6 address.
* This class is immutable.
*/
public final class Ip6Address implements Comparable<Ip6Address> {
private final long valueHigh; // The higher (more significant) 64 bits
private final long valueLow; // The lower (less significant) 64 bits
/** The length of the address in bytes (octets). */
public static final int BYTE_LENGTH = 16;
/** The length of the address in bits. */
public static final int BIT_LENGTH = BYTE_LENGTH * Byte.SIZE;
/**
* Default constructor.
*/
public Ip6Address() {
this.valueHigh = 0;
this.valueLow = 0;
}
/**
* Copy constructor.
*
* @param other the object to copy from
*/
public Ip6Address(Ip6Address other) {
this.valueHigh = other.valueHigh;
this.valueLow = other.valueLow;
}
/**
* Constructor from integer values.
*
* @param valueHigh the higher (more significant) 64 bits of the address
* @param valueLow the lower (less significant) 64 bits of the address
*/
public Ip6Address(long valueHigh, long valueLow) {
this.valueHigh = valueHigh;
this.valueLow = valueLow;
}
/**
* Constructor from a byte array with the IPv6 address stored in network
* byte order (i.e., the most significant byte first).
*
* @param value the value to use
*/
public Ip6Address(byte[] value) {
this(value, 0);
}
/**
* Constructor from a byte array with the IPv6 address stored in network
* byte order (i.e., the most significant byte first), and a given offset
* from the beginning of the byte array.
*
* @param value the value to use
* @param offset the offset in bytes from the beginning of the byte array
*/
public Ip6Address(byte[] value, int offset) {
checkNotNull(value);
// Verify the arguments
if ((offset < 0) || (offset + BYTE_LENGTH > value.length)) {
String msg;
if (value.length < BYTE_LENGTH) {
msg = "Invalid IPv6 address array: array length: " +
value.length + ". Must be at least " + BYTE_LENGTH;
} else {
msg = "Invalid IPv6 address array: array offset: " +
offset + ". Must be in the interval [0, " +
(value.length - BYTE_LENGTH) + "]";
}
throw new IllegalArgumentException(msg);
}
// Read the address
ByteBuffer bb = ByteBuffer.wrap(value);
bb.position(offset);
this.valueHigh = bb.getLong();
this.valueLow = bb.getLong();
}
/**
* Constructs an IPv6 address from a string representation of the address.
*<p>
* Example: "1111:2222::8888"
*
* @param value the value to use
*/
public Ip6Address(String value) {
checkNotNull(value);
if (value.isEmpty()) {
final String msg = "Specified IPv6 cannot be an empty string";
throw new IllegalArgumentException(msg);
}
InetAddress addr = null;
try {
addr = InetAddresses.forString(value);
} catch (IllegalArgumentException e) {
final String msg = "Invalid IPv6 address string: " + value;
throw new IllegalArgumentException(msg);
}
byte[] bytes = addr.getAddress();
ByteBuffer bb = ByteBuffer.wrap(bytes);
this.valueHigh = bb.getLong();
this.valueLow = bb.getLong();
}
/**
* Gets the IPv6 address as a byte array.
*
* @return a byte array with the IPv6 address stored in network byte order
* (i.e., the most significant byte first).
*/
public byte[] toOctets() {
return ByteBuffer.allocate(BYTE_LENGTH)
.putLong(valueHigh).putLong(valueLow).array();
}
/**
* Creates an IPv6 network mask prefix.
*
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 128].
* @return a new IPv6 address that contains a mask prefix of the
* specified length
*/
public static Ip6Address makeMaskPrefix(int prefixLen) {
long vh, vl;
// Verify the prefix length
if ((prefixLen < 0) || (prefixLen > Ip6Address.BIT_LENGTH)) {
final String msg = "Invalid IPv6 prefix length: " + prefixLen +
". Must be in the interval [0, 128].";
throw new IllegalArgumentException(msg);
}
if (prefixLen == 0) {
//
// NOTE: Apparently, the result of "<< 64" shifting to the left
// results in all 1s instead of all 0s, hence we handle it as
// a special case.
//
vh = 0;
vl = 0;
} else if (prefixLen <= 64) {
vh = (0xffffffffffffffffL << (64 - prefixLen)) & 0xffffffffffffffffL;
vl = 0;
} else {
vh = -1L; // All 1s
vl = (0xffffffffffffffffL << (128 - prefixLen)) & 0xffffffffffffffffL;
}
return new Ip6Address(vh, vl);
}
/**
* Creates an IPv6 address by masking it with a network mask of given
* mask length.
*
* @param addr the address to mask
* @param prefixLen the length of the mask prefix. Must be in the interval
* [0, 128].
* @return a new IPv6 address that is masked with a mask prefix of the
* specified length
*/
public static Ip6Address makeMaskedAddress(final Ip6Address addr,
int prefixLen) {
Ip6Address mask = Ip6Address.makeMaskPrefix(prefixLen);
long vh = addr.valueHigh & mask.valueHigh;
long vl = addr.valueLow & mask.valueLow;
return new Ip6Address(vh, vl);
}
/**
* Gets the value of the higher (more significant) 64 bits of the address.
*
* @return the value of the higher (more significant) 64 bits of the
* address
*/
public long getValueHigh() {
return valueHigh;
}
/**
* Gets the value of the lower (less significant) 64 bits of the address.
*
* @return the value of the lower (less significant) 64 bits of the
* address
*/
public long getValueLow() {
return valueLow;
}
/**
* Converts the IPv6 value to a ':' separated string.
*
* @return the IPv6 value as a ':' separated string
*/
@Override
public String toString() {
ByteBuffer bb = ByteBuffer.allocate(Ip6Address.BYTE_LENGTH);
bb.putLong(valueHigh);
bb.putLong(valueLow);
InetAddress inetAddr = null;
try {
inetAddr = InetAddress.getByAddress(bb.array());
} catch (UnknownHostException e) {
// Should never happen
checkState(false, "Internal error: Ip6Address.toString()");
return "::";
}
return InetAddresses.toAddrString(inetAddr);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Ip6Address)) {
return false;
}
Ip6Address other = (Ip6Address) o;
return this.valueHigh == other.valueHigh
&& this.valueLow == other.valueLow;
}
@Override
public int hashCode() {
return Objects.hash(valueHigh, valueLow);
}
@Override
public int compareTo(Ip6Address o) {
// Compare the high-order 64-bit value
if (this.valueHigh != o.valueHigh) {
return UnsignedLongs.compare(this.valueHigh, o.valueHigh);
}
// Compare the low-order 64-bit value
if (this.valueLow != o.valueLow) {
return UnsignedLongs.compare(this.valueLow, o.valueLow);
}
return 0;
}
}