blob: fab20aab662a98c7751289b503e4851e608f4ef3 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Ronald Liffa80792014-07-04 17:50:49 -07003import java.net.Inet4Address;
Ronald Liaf8d3e72014-07-05 01:26:28 -07004import java.net.InetAddress;
Andreas Wundsam5f71b412014-02-18 12:56:35 -08005import java.util.Arrays;
6
Andreas Wundsam3700d162014-03-11 04:43:38 -07007import javax.annotation.Nonnull;
8
Yotam Harcholf3f11152013-09-05 16:47:16 -07009import org.jboss.netty.buffer.ChannelBuffer;
10
Sovietaced9dfc1ef2014-06-27 11:13:57 -070011import com.google.common.base.Preconditions;
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070012import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -070013import com.google.common.primitives.UnsignedInts;
14
Yotam Harcholf3f11152013-09-05 16:47:16 -070015
16
17/**
Yotam Harchola289d552013-09-16 10:10:40 -070018 * Wrapper around an IPv4Address address
Yotam Harcholf3f11152013-09-05 16:47:16 -070019 *
20 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
21 */
Yotam Harchol4d634682013-09-26 13:21:06 -070022public class IPv4Address extends IPAddress<IPv4Address> {
Yotam Harcholf3f11152013-09-05 16:47:16 -070023 static final int LENGTH = 4;
24 private final int rawValue;
Yotam Harcholf3f11152013-09-05 16:47:16 -070025
Gregor Maier1acb4502013-12-12 11:25:07 -080026 private static final int NOT_A_CIDR_MASK = -1;
27 private static final int CIDR_MASK_CACHE_UNSET = -2;
Gregor Maier5615b6c2013-12-11 22:29:07 -080028 // Must appear before the static IPv4Address constant assignments
29 private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET;
30
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070031 private final static int NONE_VAL = 0x0;
32 public final static IPv4Address NONE = new IPv4Address(NONE_VAL);
33
Yotam Harchola289d552013-09-16 10:10:40 -070034 public static final IPv4Address NO_MASK = IPv4Address.of(0xFFFFFFFF);
35 public static final IPv4Address FULL_MASK = IPv4Address.of(0x00000000);
36
37 private IPv4Address(final int rawValue) {
Yotam Harcholf3f11152013-09-05 16:47:16 -070038 this.rawValue = rawValue;
39 }
40
Yotam Harchol4d634682013-09-26 13:21:06 -070041 @Override
Yotam Harcholeb023dc2013-09-26 15:45:44 -070042 public IPVersion getIpVersion() {
43 return IPVersion.IPv4;
Yotam Harchol4d634682013-09-26 13:21:06 -070044 }
45
Gregor Maier5615b6c2013-12-11 22:29:07 -080046 private int asCidrMaskLengthInternal() {
47 if (cidrMaskLengthCache == CIDR_MASK_CACHE_UNSET) {
48 // No lock required. We only write cidrMaskLengthCache once
49 int maskint = getInt();
50 if (maskint == 0) {
51 cidrMaskLengthCache = 0;
52 } else if (Integer.bitCount((~maskint) + 1) == 1) {
53 // IP represents a true CIDR prefix length
54 cidrMaskLengthCache = Integer.bitCount(maskint);
55 } else {
56 cidrMaskLengthCache = NOT_A_CIDR_MASK;
57 }
58 }
59 return cidrMaskLengthCache;
60 }
61
62 @Override
63 public boolean isCidrMask() {
64 return asCidrMaskLengthInternal() != NOT_A_CIDR_MASK;
65 }
Gregor Maier7f987e62013-12-10 19:34:18 -080066
67 @Override
68 public int asCidrMaskLength() {
Gregor Maier5615b6c2013-12-11 22:29:07 -080069 if (!isCidrMask()) {
70 throw new IllegalStateException("IP is not a valid CIDR prefix " +
71 "mask " + toString());
Gregor Maier7f987e62013-12-10 19:34:18 -080072 } else {
Gregor Maier5615b6c2013-12-11 22:29:07 -080073 return asCidrMaskLengthInternal();
Gregor Maier7f987e62013-12-10 19:34:18 -080074 }
75 }
76
Aditya Vaja56b8b182014-03-11 13:13:58 -070077 @Override
78 public boolean isBroadcast() {
79 return this.equals(NO_MASK);
80 }
81
82 @Override
Aditya Vaja98c96e72014-03-11 15:19:01 -070083 public IPv4Address and(IPv4Address other) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070084 Preconditions.checkNotNull(other, "other must not be null");
85
86 IPv4Address otherIp = other;
Aditya Vaja56b8b182014-03-11 13:13:58 -070087 return IPv4Address.of(rawValue & otherIp.rawValue);
88 }
89
90 @Override
Aditya Vaja98c96e72014-03-11 15:19:01 -070091 public IPv4Address or(IPv4Address other) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -070092 Preconditions.checkNotNull(other, "other must not be null");
93
94 IPv4Address otherIp = other;
Aditya Vaja56b8b182014-03-11 13:13:58 -070095 return IPv4Address.of(rawValue | otherIp.rawValue);
96 }
97
98 @Override
99 public IPv4Address not() {
100 return IPv4Address.of(~rawValue);
101 }
102
Ronald Liaf8d3e72014-07-05 01:26:28 -0700103 /**
104 * Returns an {@code IPv4Address} object that represents the given
105 * raw IP address. The argument is in network byte order: the highest
106 * order byte of the address is in {@code address[0]}.
107 * <p>
108 * The address byte array must be 4 bytes long (32 bits long).
109 * <p>
110 * Similar to {@link InetAddress#getByAddress(byte[])}.
111 *
112 * @param address the raw IP address in network byte order
113 * @return an {@code IPv4Address} object that represents the given
114 * raw IP address
115 * @throws NullPointerException if the given address was {@code null}
116 * @throws IllegalArgumentException if the given address was of an invalid
117 * byte array length
118 * @see InetAddress#getByAddress(byte[])
119 */
Ronald Liffa80792014-07-04 17:50:49 -0700120 @Nonnull
121 public static IPv4Address of(@Nonnull final byte[] address) {
Sovietaced9dfc1ef2014-06-27 11:13:57 -0700122 Preconditions.checkNotNull(address, "address must not be null");
123
Yotam Harcholf3f11152013-09-05 16:47:16 -0700124 if (address.length != LENGTH) {
125 throw new IllegalArgumentException(
Andreas Wundsamc85b5c52013-09-24 13:01:43 -0700126 "Invalid byte array length for IPv4Address address: " + address.length);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700127 }
128
129 int raw =
130 (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16
131 | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0;
Yotam Harchola289d552013-09-16 10:10:40 -0700132 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700133 }
134
Ronald Liaf8d3e72014-07-05 01:26:28 -0700135 /**
136 * Returns an {@code IPv4Address} object that represents the given
137 * raw IP address represented as a 32-bit integer.
Andreas Wundsam3700d162014-03-11 04:43:38 -0700138 *
Ronald Liaf8d3e72014-07-05 01:26:28 -0700139 * @param raw the raw IP address represented as a 32-bit integer
140 * @return an {@code IPv4Address} object that represents the given
141 * raw IP address
Andreas Wundsam3700d162014-03-11 04:43:38 -0700142 */
Ronald Liffa80792014-07-04 17:50:49 -0700143 @Nonnull
Yotam Harchola289d552013-09-16 10:10:40 -0700144 public static IPv4Address of(final int raw) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700145 if(raw == NONE_VAL)
146 return NONE;
Yotam Harchola289d552013-09-16 10:10:40 -0700147 return new IPv4Address(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700148 }
149
Ronald Liaf8d3e72014-07-05 01:26:28 -0700150 /**
151 * Returns an {@code IPv4Address} object that represents the given
152 * IP address in the canonical dotted-quad notation. For example,
153 * {@code 1.2.3.4}.
Andreas Wundsam3700d162014-03-11 04:43:38 -0700154 *
Ronald Liaf8d3e72014-07-05 01:26:28 -0700155 * @param string the IP address in the dotted-quad notation
156 * @return an {@code IPv4Address} object that represents the given
157 * dotted-quad textual IP address
158 * @throws NullPointerException if given string was {@code null}
159 * @throws IllegalArgumentException if given string was not a valid
160 * IPv4 address
Andreas Wundsam3700d162014-03-11 04:43:38 -0700161 */
162 @Nonnull
163 public static IPv4Address of(@Nonnull final String string) throws IllegalArgumentException {
Sovietaced9dfc1ef2014-06-27 11:13:57 -0700164 Preconditions.checkNotNull(string, "string must not be null");
165
Yotam Harcholf3f11152013-09-05 16:47:16 -0700166 int start = 0;
167 int shift = 24;
168
169 int raw = 0;
170 while (shift >= 0) {
171 int end = string.indexOf('.', start);
172 if (end == start || !((shift > 0) ^ (end < 0)))
173 throw new IllegalArgumentException("IP Address not well formed: " + string);
174
175 String substr =
176 end > 0 ? string.substring(start, end) : string.substring(start);
177 int val = Integer.parseInt(substr);
178 if (val < 0 || val > 255)
179 throw new IllegalArgumentException("IP Address not well formed: " + string);
180
181 raw |= val << shift;
182
183 shift -= 8;
184 start = end + 1;
185 }
Yotam Harchola289d552013-09-16 10:10:40 -0700186 return IPv4Address.of(raw);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700187 }
188
Ronald Liaf8d3e72014-07-05 01:26:28 -0700189 /**
190 * Returns an {@code IPv4Address} object that represents the
191 * IP address represented by the given {@code Inet4Address} object.
192 *
193 * @param address the {@code Inet4Address} object
194 * @return an {@code IPv4Address} object that represents the
195 * given IP address
196 * @throws NullPointerException if the {@code Inet4Address} object
197 * was {@code null}
198 */
Ronald Liffa80792014-07-04 17:50:49 -0700199 @Nonnull
200 public static IPv4Address of(@Nonnull final Inet4Address address) {
201 Preconditions.checkNotNull(address, "address must not be null");
202 return IPv4Address.of(address.getAddress());
203 }
204
Ronald Liaf8d3e72014-07-05 01:26:28 -0700205 /**
206 * Returns an {@code IPv4Address} object that represents the
207 * CIDR subnet mask of the given prefix length, that is, the
208 * number of leading one-bits.
209 *
210 * @param cidrMaskLength the prefix length of the CIDR subnet mask,
211 * where {@code 0 <= cidrMaskLength <= 32}
212 * @return an {@code IPv4Address} object that represents the
213 * CIDR subnet mask of the given prefix length
214 * @throws IllegalArgumentException if the given prefix length was invalid
215 */
Ronald Liffa80792014-07-04 17:50:49 -0700216 @Nonnull
Ronald Lia7484222014-07-03 17:14:12 -0700217 public static IPv4Address ofCidrMaskLength(final int cidrMaskLength) {
218 Preconditions.checkArgument(
219 cidrMaskLength >= 0 && cidrMaskLength <= 32,
220 "Invalid IPv4 CIDR mask length: %s", cidrMaskLength);
221
222 if (cidrMaskLength == 32) {
223 return IPv4Address.NO_MASK;
224 } else if (cidrMaskLength == 0) {
225 return IPv4Address.FULL_MASK;
226 } else {
227 int mask = (-1) << (32 - cidrMaskLength);
228 return IPv4Address.of(mask);
229 }
230 }
231
Ronald Liaf8d3e72014-07-05 01:26:28 -0700232 /**
233 * Returns an {@code IPv4AddressWithMask} object that represents this
234 * IP address masked by the given raw IP address mask. The argument is in
235 * network byte order: the highest order byte of the address is in
236 * {@code mask[0]}.
237 * <p>
238 * The address byte array must be 4 bytes long (32 bits long).
239 *
240 * @param mask the raw IP address mask in network byte order
241 * @return an {@code IPv4AddressWithMask} object that represents this
242 * IP address masked by the given raw IP address mask
243 * @throws NullPointerException if the given mask was {@code null}
244 * @throws IllegalArgumentException if the given mask was of an invalid
245 * byte array length
246 * @see #of(byte[])
247 */
248 @Nonnull
249 public IPv4AddressWithMask withMask(@Nonnull final byte[] mask) {
Ronald Liffa80792014-07-04 17:50:49 -0700250 return IPv4AddressWithMask.of(this, IPv4Address.of(mask));
251 }
252
Ronald Liaf8d3e72014-07-05 01:26:28 -0700253 /**
254 * Returns an {@code IPv4AddressWithMask} object that represents this
255 * IP address masked by the given raw IP address mask represented as a
256 * 32-bit integer.
257 *
258 * @param mask the raw IP address mask represented as a 32-bit integer
259 * @return an {@code IPv4AddressWithMask} object that represents this
260 * IP address masked by the given raw IP address mask
261 * @see #of(int)
262 */
263 @Nonnull
264 public IPv4AddressWithMask withMask(final int mask) {
Ronald Liffa80792014-07-04 17:50:49 -0700265 return IPv4AddressWithMask.of(this, IPv4Address.of(mask));
266 }
267
Ronald Liaf8d3e72014-07-05 01:26:28 -0700268 /**
269 * Returns an {@code IPv4AddressWithMask} object that represents this
270 * IP address masked by the given IP address mask in the canonical
271 * dotted-quad notation. For example, {@code 255.255.255.0}.
272 *
273 * @param mask the IP address mask in the dotted-quad notation
274 * @return an {@code IPv4AddressWithMask} object that represents this
275 * IP address masked by the given dotted-quad textual IP
276 * address mask
277 * @throws NullPointerException if the given string was {@code null}
278 * @throws IllegalArgumentException if the given string was not a valid
279 * IPv4 address mask
280 * @see #of(String)
281 */
282 @Nonnull
283 public IPv4AddressWithMask withMask(@Nonnull final String mask) {
Ronald Liffa80792014-07-04 17:50:49 -0700284 return IPv4AddressWithMask.of(this, IPv4Address.of(mask));
285 }
286
Ronald Liaf8d3e72014-07-05 01:26:28 -0700287 /**
288 * Returns an {@code IPv4AddressWithMask} object that represents this
289 * IP address masked by the IP address mask represented by the given
290 * {@code Inet4Address} object.
291 *
292 * @param mask the {@code Inet4Address} object
293 * @return an {@code IPv4AddressWithMask} object that represents this
294 * IP address masked by the given IP address mask
295 * @throws NullPointerException if the {@code Inet4Address} object
296 * was {@code null}
297 * @see #of(Inet4Address)
298 */
299 @Nonnull
300 public IPv4AddressWithMask withMask(@Nonnull final Inet4Address mask) {
Ronald Liffa80792014-07-04 17:50:49 -0700301 return IPv4AddressWithMask.of(this, IPv4Address.of(mask));
302 }
303
Ronald Liaf8d3e72014-07-05 01:26:28 -0700304 /**
305 * Returns an {@code IPv4AddressWithMask} object that represents this
306 * IP address masked by the CIDR subnet mask of the given prefix length,
307 * that is, the number of leading one-bits.
308 *
309 * @param cidrMaskLength the prefix length of the CIDR subnet mask,
310 * where {@code 0 <= cidrMaskLength <= 32}
311 * @return an {@code IPv4AddressWithMask} object that
312 * represents this IP address masked by the CIDR
313 * subnet mask of the given prefix length
314 * @throws IllegalArgumentException if the given prefix length was invalid
315 * @see #ofCidrMaskLength(int)
316 */
317 @Nonnull
318 public IPv4AddressWithMask withMaskOfLength(final int cidrMaskLength) {
Ronald Liffa80792014-07-04 17:50:49 -0700319 return IPv4AddressWithMask.of(this,
320 IPv4Address.ofCidrMaskLength(cidrMaskLength));
321 }
322
Yotam Harcholf3f11152013-09-05 16:47:16 -0700323 public int getInt() {
324 return rawValue;
325 }
326
Andreas Wundsam4e2469e2014-02-17 15:32:43 -0800327 private volatile byte[] bytesCache = null;
Yotam Harcholf3f11152013-09-05 16:47:16 -0700328
Ronald Liaf8d3e72014-07-05 01:26:28 -0700329 @Override
Yotam Harcholf3f11152013-09-05 16:47:16 -0700330 public byte[] getBytes() {
331 if (bytesCache == null) {
332 synchronized (this) {
333 if (bytesCache == null) {
334 bytesCache =
335 new byte[] { (byte) ((rawValue >>> 24) & 0xFF),
336 (byte) ((rawValue >>> 16) & 0xFF),
337 (byte) ((rawValue >>> 8) & 0xFF),
338 (byte) ((rawValue >>> 0) & 0xFF) };
339 }
340 }
341 }
Andreas Wundsam5f71b412014-02-18 12:56:35 -0800342 return Arrays.copyOf(bytesCache, bytesCache.length);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700343 }
344
345 @Override
346 public int getLength() {
347 return LENGTH;
348 }
349
350 @Override
351 public String toString() {
352 StringBuilder res = new StringBuilder();
353 res.append((rawValue >> 24) & 0xFF).append('.');
354 res.append((rawValue >> 16) & 0xFF).append('.');
355 res.append((rawValue >> 8) & 0xFF).append('.');
356 res.append((rawValue >> 0) & 0xFF);
357 return res.toString();
358 }
359
Yotam Harcholf3f11152013-09-05 16:47:16 -0700360 public void write4Bytes(ChannelBuffer c) {
361 c.writeInt(rawValue);
362 }
Yotam Harchola289d552013-09-16 10:10:40 -0700363
364 public static IPv4Address read4Bytes(ChannelBuffer c) {
365 return IPv4Address.of(c.readInt());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700366 }
367
368 @Override
Yotam Harchola289d552013-09-16 10:10:40 -0700369 public IPv4Address applyMask(IPv4Address mask) {
Aditya Vaja98c96e72014-03-11 15:19:01 -0700370 return and(mask);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700371 }
372
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700373 @Override
374 public int hashCode() {
375 final int prime = 31;
376 int result = 1;
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700377 result = prime * result + rawValue;
378 return result;
379 }
Yotam Harchola289d552013-09-16 10:10:40 -0700380
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700381 @Override
382 public boolean equals(Object obj) {
383 if (this == obj)
384 return true;
385 if (obj == null)
386 return false;
387 if (getClass() != obj.getClass())
388 return false;
389 IPv4Address other = (IPv4Address) obj;
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700390 if (rawValue != other.rawValue)
391 return false;
392 return true;
393 }
394
395 @Override
396 public int compareTo(IPv4Address o) {
397 return UnsignedInts.compare(rawValue, o.rawValue);
398 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700399
400 @Override
401 public void putTo(PrimitiveSink sink) {
402 sink.putInt(rawValue);
403 }
404
Yotam Harcholf3f11152013-09-05 16:47:16 -0700405}