Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 4 | import java.net.InetAddress; |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 5 | import java.util.Arrays; |
| 6 | |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 7 | import javax.annotation.Nonnull; |
| 8 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | import org.jboss.netty.buffer.ChannelBuffer; |
| 10 | |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 11 | import com.google.common.base.Preconditions; |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 12 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 13 | import com.google.common.primitives.UnsignedInts; |
| 14 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 15 | |
| 16 | |
| 17 | /** |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 18 | * Wrapper around an IPv4Address address |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 19 | * |
| 20 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 21 | */ |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 22 | public class IPv4Address extends IPAddress<IPv4Address> { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 23 | static final int LENGTH = 4; |
| 24 | private final int rawValue; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 25 | |
Gregor Maier | 1acb450 | 2013-12-12 11:25:07 -0800 | [diff] [blame] | 26 | private static final int NOT_A_CIDR_MASK = -1; |
| 27 | private static final int CIDR_MASK_CACHE_UNSET = -2; |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 28 | // Must appear before the static IPv4Address constant assignments |
| 29 | private volatile int cidrMaskLengthCache = CIDR_MASK_CACHE_UNSET; |
| 30 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 31 | private final static int NONE_VAL = 0x0; |
| 32 | public final static IPv4Address NONE = new IPv4Address(NONE_VAL); |
| 33 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 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) { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 38 | this.rawValue = rawValue; |
| 39 | } |
| 40 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 41 | @Override |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 42 | public IPVersion getIpVersion() { |
| 43 | return IPVersion.IPv4; |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 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 | } |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 66 | |
| 67 | @Override |
| 68 | public int asCidrMaskLength() { |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 69 | if (!isCidrMask()) { |
| 70 | throw new IllegalStateException("IP is not a valid CIDR prefix " + |
| 71 | "mask " + toString()); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 72 | } else { |
Gregor Maier | 5615b6c | 2013-12-11 22:29:07 -0800 | [diff] [blame] | 73 | return asCidrMaskLengthInternal(); |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public boolean isBroadcast() { |
| 79 | return this.equals(NO_MASK); |
| 80 | } |
| 81 | |
| 82 | @Override |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 83 | public IPv4Address and(IPv4Address other) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 84 | Preconditions.checkNotNull(other, "other must not be null"); |
| 85 | |
| 86 | IPv4Address otherIp = other; |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -0700 | [diff] [blame] | 87 | return IPv4Address.of(rawValue & otherIp.rawValue); |
| 88 | } |
| 89 | |
| 90 | @Override |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 91 | public IPv4Address or(IPv4Address other) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 92 | Preconditions.checkNotNull(other, "other must not be null"); |
| 93 | |
| 94 | IPv4Address otherIp = other; |
Aditya Vaja | 56b8b18 | 2014-03-11 13:13:58 -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 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 105 | * IP address. The argument is in network byte order: the highest |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 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 Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 120 | @Nonnull |
| 121 | public static IPv4Address of(@Nonnull final byte[] address) { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 122 | Preconditions.checkNotNull(address, "address must not be null"); |
| 123 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 124 | if (address.length != LENGTH) { |
| 125 | throw new IllegalArgumentException( |
Ronald Li | 74b54bc | 2014-07-06 23:59:13 -0700 | [diff] [blame] | 126 | "Invalid byte array length for IPv4 address: " + address.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -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; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 132 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 137 | * IP address. |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 138 | * |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -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 |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 142 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 143 | @Nonnull |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 144 | public static IPv4Address of(final int raw) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 145 | if(raw == NONE_VAL) |
| 146 | return NONE; |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 147 | return new IPv4Address(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Returns an {@code IPv4Address} object that represents the given |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 152 | * IP address. The argument is in the canonical quad-dotted notation. |
| 153 | * For example, {@code 1.2.3.4}. |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 154 | * |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 155 | * @param string the IP address in the canonical quad-dotted notation |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 156 | * @return an {@code IPv4Address} object that represents the given |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 157 | * IP address |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 158 | * @throws NullPointerException if the given string was {@code null} |
| 159 | * @throws IllegalArgumentException if the given string was not a valid |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 160 | * IPv4 address |
Andreas Wundsam | 3700d16 | 2014-03-11 04:43:38 -0700 | [diff] [blame] | 161 | */ |
| 162 | @Nonnull |
| 163 | public static IPv4Address of(@Nonnull final String string) throws IllegalArgumentException { |
Sovietaced | 9dfc1ef | 2014-06-27 11:13:57 -0700 | [diff] [blame] | 164 | Preconditions.checkNotNull(string, "string must not be null"); |
| 165 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -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 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 186 | return IPv4Address.of(raw); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 189 | /** |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 190 | * Returns an {@code IPv4Address} object that represents the given |
| 191 | * IP address. The argument is given as an {@code Inet4Address} object. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 192 | * |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 193 | * @param address the IP address as an {@code Inet4Address} object |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 194 | * @return an {@code IPv4Address} object that represents the |
| 195 | * given IP address |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 196 | * @throws NullPointerException if the given {@code Inet4Address} was |
| 197 | * {@code null} |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 198 | */ |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 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 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Returns an {@code IPv4Address} object that represents the |
Ronald Li | c3c4a95 | 2014-07-07 01:01:46 -0700 | [diff] [blame] | 207 | * CIDR subnet mask of the given prefix length. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 208 | * |
Ronald Li | c3c4a95 | 2014-07-07 01:01:46 -0700 | [diff] [blame] | 209 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 210 | * (i.e. the number of leading one-bits), |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 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 Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 216 | @Nonnull |
Ronald Li | a748422 | 2014-07-03 17:14:12 -0700 | [diff] [blame] | 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 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | ca08d5f | 2014-07-05 02:43:18 -0700 | [diff] [blame] | 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 | public IPv4AddressWithMask withMask(@Nonnull final IPv4Address mask) { |
| 243 | return IPv4AddressWithMask.of(this, mask); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 248 | * IP address masked by the given IP address mask. The argument is in |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 249 | * network byte order: the highest order byte of the address is in |
| 250 | * {@code mask[0]}. |
| 251 | * <p> |
| 252 | * The address byte array must be 4 bytes long (32 bits long). |
| 253 | * |
| 254 | * @param mask the raw IP address mask in network byte order |
| 255 | * @return an {@code IPv4AddressWithMask} object that represents this |
| 256 | * IP address masked by the given raw IP address mask |
| 257 | * @throws NullPointerException if the given mask was {@code null} |
| 258 | * @throws IllegalArgumentException if the given mask was of an invalid |
| 259 | * byte array length |
| 260 | * @see #of(byte[]) |
| 261 | */ |
| 262 | @Nonnull |
| 263 | public IPv4AddressWithMask withMask(@Nonnull final byte[] mask) { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 264 | return IPv4AddressWithMask.of(this, IPv4Address.of(mask)); |
| 265 | } |
| 266 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 269 | * IP address masked by the given IP address mask. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 270 | * |
| 271 | * @param mask the raw IP address mask represented as a 32-bit integer |
| 272 | * @return an {@code IPv4AddressWithMask} object that represents this |
| 273 | * IP address masked by the given raw IP address mask |
| 274 | * @see #of(int) |
| 275 | */ |
| 276 | @Nonnull |
| 277 | public IPv4AddressWithMask withMask(final int mask) { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 278 | return IPv4AddressWithMask.of(this, IPv4Address.of(mask)); |
| 279 | } |
| 280 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 283 | * IP address masked by the given IP address mask. The argument is in |
| 284 | * the canonical quad-dotted notation. For example, {@code 255.255.255.0}. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 285 | * |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 286 | * @param mask the IP address mask in the canonical quad-dotted notation |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 287 | * @return an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 288 | * IP address masked by the given IP address mask |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 289 | * @throws NullPointerException if the given string was {@code null} |
| 290 | * @throws IllegalArgumentException if the given string was not a valid |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 291 | * IPv4 address |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 292 | * @see #of(String) |
| 293 | */ |
| 294 | @Nonnull |
| 295 | public IPv4AddressWithMask withMask(@Nonnull final String mask) { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 296 | return IPv4AddressWithMask.of(this, IPv4Address.of(mask)); |
| 297 | } |
| 298 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 097cda5 | 2014-07-06 23:17:05 -0700 | [diff] [blame] | 301 | * IP address masked by the given IP address mask. The argument is given as |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 302 | * an {@code Inet4Address} object. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 303 | * |
Ronald Li | 382589f | 2014-07-06 23:58:08 -0700 | [diff] [blame] | 304 | * @param mask the IP address mask as an {@code Inet4Address} object |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 305 | * @return an {@code IPv4AddressWithMask} object that represents this |
| 306 | * IP address masked by the given IP address mask |
Ronald Li | 5b41da9 | 2014-07-06 22:59:28 -0700 | [diff] [blame] | 307 | * @throws NullPointerException if the given {@code Inet4Address} was |
| 308 | * {@code null} |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 309 | * @see #of(Inet4Address) |
| 310 | */ |
| 311 | @Nonnull |
| 312 | public IPv4AddressWithMask withMask(@Nonnull final Inet4Address mask) { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 313 | return IPv4AddressWithMask.of(this, IPv4Address.of(mask)); |
| 314 | } |
| 315 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Returns an {@code IPv4AddressWithMask} object that represents this |
Ronald Li | 383d14e | 2014-07-07 01:09:18 -0700 | [diff] [blame] | 318 | * IP address masked by the CIDR subnet mask of the given prefix length. |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 319 | * |
Ronald Li | 383d14e | 2014-07-07 01:09:18 -0700 | [diff] [blame] | 320 | * @param cidrMaskLength the prefix length of the CIDR subnet mask |
| 321 | * (i.e. the number of leading one-bits), |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 322 | * where {@code 0 <= cidrMaskLength <= 32} |
| 323 | * @return an {@code IPv4AddressWithMask} object that |
| 324 | * represents this IP address masked by the CIDR |
| 325 | * subnet mask of the given prefix length |
| 326 | * @throws IllegalArgumentException if the given prefix length was invalid |
| 327 | * @see #ofCidrMaskLength(int) |
| 328 | */ |
| 329 | @Nonnull |
| 330 | public IPv4AddressWithMask withMaskOfLength(final int cidrMaskLength) { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 331 | return IPv4AddressWithMask.of(this, |
| 332 | IPv4Address.ofCidrMaskLength(cidrMaskLength)); |
| 333 | } |
| 334 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 335 | public int getInt() { |
| 336 | return rawValue; |
| 337 | } |
| 338 | |
Andreas Wundsam | 4e2469e | 2014-02-17 15:32:43 -0800 | [diff] [blame] | 339 | private volatile byte[] bytesCache = null; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 340 | |
Ronald Li | af8d3e7 | 2014-07-05 01:26:28 -0700 | [diff] [blame] | 341 | @Override |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 342 | public byte[] getBytes() { |
| 343 | if (bytesCache == null) { |
| 344 | synchronized (this) { |
| 345 | if (bytesCache == null) { |
| 346 | bytesCache = |
| 347 | new byte[] { (byte) ((rawValue >>> 24) & 0xFF), |
| 348 | (byte) ((rawValue >>> 16) & 0xFF), |
| 349 | (byte) ((rawValue >>> 8) & 0xFF), |
| 350 | (byte) ((rawValue >>> 0) & 0xFF) }; |
| 351 | } |
| 352 | } |
| 353 | } |
Andreas Wundsam | 5f71b41 | 2014-02-18 12:56:35 -0800 | [diff] [blame] | 354 | return Arrays.copyOf(bytesCache, bytesCache.length); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public int getLength() { |
| 359 | return LENGTH; |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public String toString() { |
| 364 | StringBuilder res = new StringBuilder(); |
| 365 | res.append((rawValue >> 24) & 0xFF).append('.'); |
| 366 | res.append((rawValue >> 16) & 0xFF).append('.'); |
| 367 | res.append((rawValue >> 8) & 0xFF).append('.'); |
| 368 | res.append((rawValue >> 0) & 0xFF); |
| 369 | return res.toString(); |
| 370 | } |
| 371 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 372 | public void write4Bytes(ChannelBuffer c) { |
| 373 | c.writeInt(rawValue); |
| 374 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 375 | |
| 376 | public static IPv4Address read4Bytes(ChannelBuffer c) { |
| 377 | return IPv4Address.of(c.readInt()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 381 | public IPv4Address applyMask(IPv4Address mask) { |
Aditya Vaja | 98c96e7 | 2014-03-11 15:19:01 -0700 | [diff] [blame] | 382 | return and(mask); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public int hashCode() { |
| 387 | final int prime = 31; |
| 388 | int result = 1; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 389 | result = prime * result + rawValue; |
| 390 | return result; |
| 391 | } |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 392 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 393 | @Override |
| 394 | public boolean equals(Object obj) { |
| 395 | if (this == obj) |
| 396 | return true; |
| 397 | if (obj == null) |
| 398 | return false; |
| 399 | if (getClass() != obj.getClass()) |
| 400 | return false; |
| 401 | IPv4Address other = (IPv4Address) obj; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 402 | if (rawValue != other.rawValue) |
| 403 | return false; |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public int compareTo(IPv4Address o) { |
| 409 | return UnsignedInts.compare(rawValue, o.rawValue); |
| 410 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 411 | |
| 412 | @Override |
| 413 | public void putTo(PrimitiveSink sink) { |
| 414 | sink.putInt(rawValue); |
| 415 | } |
| 416 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 417 | } |