alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
| 4 | import java.net.InetAddress; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 5 | import java.util.Arrays; |
| 6 | |
| 7 | import javax.annotation.Nonnull; |
| 8 | |
| 9 | import org.jboss.netty.buffer.ChannelBuffer; |
| 10 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 11 | import com.google.common.base.Preconditions; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 12 | import com.google.common.hash.PrimitiveSink; |
| 13 | import com.google.common.primitives.UnsignedInts; |
| 14 | |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Wrapper around an IPv4Address address |
| 19 | * |
| 20 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 21 | */ |
| 22 | public class IPv4Address extends IPAddress<IPv4Address> { |
| 23 | static final int LENGTH = 4; |
| 24 | private final int rawValue; |
| 25 | |
| 26 | private static final int NOT_A_CIDR_MASK = -1; |
| 27 | private static final int CIDR_MASK_CACHE_UNSET = -2; |
| 28 | // Must appear before the static IPv4Address constant assignments |
| 29 | private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET; |
| 30 | |
| 31 | private final static int NONE_VAL = 0x0; |
| 32 | public final static IPv4Address NONE = new IPv4Address(NONE_VAL); |
| 33 | |
| 34 | 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) { |
| 38 | this.rawValue = rawValue; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public IPVersion getIpVersion() { |
| 43 | return IPVersion.IPv4; |
| 44 | } |
| 45 | |
| 46 | 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 | } |
| 66 | |
| 67 | @Override |
| 68 | public int asCidrMaskLength() { |
| 69 | if (!isCidrMask()) { |
| 70 | throw new IllegalStateException("IP is not a valid CIDR prefix " + |
| 71 | "mask " + toString()); |
| 72 | } else { |
| 73 | return asCidrMaskLengthInternal(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean isBroadcast() { |
| 79 | return this.equals(NO_MASK); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public IPv4Address and(IPv4Address other) { |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 84 | Preconditions.checkNotNull(other, "other must not be null"); |
| 85 | |
| 86 | IPv4Address otherIp = other; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 87 | return IPv4Address.of(rawValue & otherIp.rawValue); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public IPv4Address or(IPv4Address other) { |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 92 | Preconditions.checkNotNull(other, "other must not be null"); |
| 93 | |
| 94 | IPv4Address otherIp = other; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 95 | return IPv4Address.of(rawValue | otherIp.rawValue); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public IPv4Address not() { |
| 100 | return IPv4Address.of(~rawValue); |
| 101 | } |
| 102 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Returns an {@code IPv4Address} object that represents the given |
| 105 | * 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 | */ |
| 120 | @Nonnull |
| 121 | public static IPv4Address of(@Nonnull final byte[] address) { |
| 122 | Preconditions.checkNotNull(address, "address must not be null"); |
| 123 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 124 | if (address.length != LENGTH) { |
| 125 | throw new IllegalArgumentException( |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 126 | "Invalid byte array length for IPv4 address: " + address.length); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int raw = |
| 130 | (address[0] & 0xFF) << 24 | (address[1] & 0xFF) << 16 |
| 131 | | (address[2] & 0xFF) << 8 | (address[3] & 0xFF) << 0; |
| 132 | return IPv4Address.of(raw); |
| 133 | } |
| 134 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns an {@code IPv4Address} object that represents the given |
| 137 | * IP address. |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 138 | * |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 139 | * @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 |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 142 | */ |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 143 | @Nonnull |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 144 | public static IPv4Address of(final int raw) { |
| 145 | if(raw == NONE_VAL) |
| 146 | return NONE; |
| 147 | return new IPv4Address(raw); |
| 148 | } |
| 149 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns an {@code IPv4Address} object that represents the given |
| 152 | * IP address. The argument is in the canonical quad-dotted notation. |
| 153 | * For example, {@code 1.2.3.4}. |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 154 | * |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 155 | * @param string the IP address in the canonical quad-dotted notation |
| 156 | * @return an {@code IPv4Address} object that represents the given |
| 157 | * IP address |
| 158 | * @throws NullPointerException if the given string was {@code null} |
| 159 | * @throws IllegalArgumentException if the given string was not a valid |
| 160 | * IPv4 address |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 161 | */ |
| 162 | @Nonnull |
| 163 | public static IPv4Address of(@Nonnull final String string) throws IllegalArgumentException { |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 164 | Preconditions.checkNotNull(string, "string must not be null"); |
| 165 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 166 | 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 | } |
| 186 | return IPv4Address.of(raw); |
| 187 | } |
| 188 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Returns an {@code IPv4Address} object that represents the given |
| 191 | * IP address. The argument is given as an {@code Inet4Address} object. |
| 192 | * |
| 193 | * @param address the IP address as an {@code Inet4Address} object |
| 194 | * @return an {@code IPv4Address} object that represents the |
| 195 | * given IP address |
| 196 | * @throws NullPointerException if the given {@code Inet4Address} was |
| 197 | * {@code null} |
| 198 | */ |
| 199 | @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 | |
| 205 | /** |
| 206 | * Returns an {@code IPv4Address} object that represents the |
| 207 | * CIDR subnet mask of the given prefix length. |
| 208 | * |
| 209 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 210 | * (i.e. the number of leading one-bits), |
| 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 | */ |
| 216 | @Nonnull |
| 217 | 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 | |
| 232 | /** |
| 233 | * Returns an {@code IPv4AddressWithMask} object that represents this |
| 234 | * IP address masked by the given IP address mask. |
| 235 | * |
| 236 | * @param mask the {@code IPv4Address} object that represents the mask |
| 237 | * @return an {@code IPv4AddressWithMask} object that represents this |
| 238 | * IP address masked by the given mask |
| 239 | * @throws NullPointerException if the given mask was {@code null} |
| 240 | */ |
| 241 | @Nonnull |
| 242 | @Override |
| 243 | public IPv4AddressWithMask withMask(@Nonnull final IPv4Address mask) { |
| 244 | return IPv4AddressWithMask.of(this, mask); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns an {@code IPv4AddressWithMask} object that represents this |
| 249 | * IP address masked by the CIDR subnet mask of the given prefix length. |
| 250 | * |
| 251 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 252 | * (i.e. the number of leading one-bits), |
| 253 | * where {@code 0 <= cidrMaskLength <= 32} |
| 254 | * @return an {@code IPv4AddressWithMask} object that |
| 255 | * represents this IP address masked by the CIDR |
| 256 | * subnet mask of the given prefix length |
| 257 | * @throws IllegalArgumentException if the given prefix length was invalid |
| 258 | * @see #ofCidrMaskLength(int) |
| 259 | */ |
| 260 | @Nonnull |
| 261 | @Override |
| 262 | public IPv4AddressWithMask withMaskOfLength(final int cidrMaskLength) { |
| 263 | return this.withMask(IPv4Address.ofCidrMaskLength(cidrMaskLength)); |
| 264 | } |
| 265 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 266 | public int getInt() { |
| 267 | return rawValue; |
| 268 | } |
| 269 | |
| 270 | private volatile byte[] bytesCache = null; |
| 271 | |
alshabib | 86ac11c | 2014-08-14 16:14:41 -0700 | [diff] [blame] | 272 | @Override |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 273 | public byte[] getBytes() { |
| 274 | if (bytesCache == null) { |
| 275 | synchronized (this) { |
| 276 | if (bytesCache == null) { |
| 277 | bytesCache = |
| 278 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 279 | (byte) ((rawValue >>> 16) & 0xFF), |
| 280 | (byte) ((rawValue >>> 8) & 0xFF), |
| 281 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return Arrays.copyOf(bytesCache, bytesCache.length); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public int getLength() { |
| 290 | return LENGTH; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public String toString() { |
| 295 | StringBuilder res = new StringBuilder(); |
| 296 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 297 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 298 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 299 | res.append((rawValue >> 0) & 0xFF); |
| 300 | return res.toString(); |
| 301 | } |
| 302 | |
| 303 | public void write4Bytes(ChannelBuffer c) { |
| 304 | c.writeInt(rawValue); |
| 305 | } |
| 306 | |
| 307 | public static IPv4Address read4Bytes(ChannelBuffer c) { |
| 308 | return IPv4Address.of(c.readInt()); |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public IPv4Address applyMask(IPv4Address mask) { |
| 313 | return and(mask); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public int hashCode() { |
| 318 | final int prime = 31; |
| 319 | int result = 1; |
| 320 | result = prime * result + rawValue; |
| 321 | return result; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public boolean equals(Object obj) { |
| 326 | if (this == obj) |
| 327 | return true; |
| 328 | if (obj == null) |
| 329 | return false; |
| 330 | if (getClass() != obj.getClass()) |
| 331 | return false; |
| 332 | IPv4Address other = (IPv4Address) obj; |
| 333 | if (rawValue != other.rawValue) |
| 334 | return false; |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public int compareTo(IPv4Address o) { |
| 340 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void putTo(PrimitiveSink sink) { |
| 345 | sink.putInt(rawValue); |
| 346 | } |
| 347 | |
| 348 | } |